#include <stdio.h>
#include <stdlib.h>
int CMP(int *a,int *b)
{
if(*a<*b)
return -1;
else if(*a>*b)
return 1;
else
return 0;
}
int main(void)
{
int search[10]={1,3,6,7,10,11,13,19,28,56} ;
int a=13,*p,i;
/*对数组search 进行二分搜索13*/
p=(int *)bsearch(&a, search,10, sizeof(int),(int(*)(const void *,const void *))CMP);
printf("The elems of the array are\n");
for(i=0;i<10;i++)
printf("%d ",search[i]);
/*显示元素13 在原数组中的位置*/
if(p)
printf("\nThe elem 13 is located at %d of the array\n",p-search+1);
else
printf("\nSearch is fail!!\n");
getchar();
}
p=(int *)bsearch(&a, search,10, sizeof(int),(int(*)(const void *,const void *))CMP);
在CMP函数前为什么还要加强制类型转换?
扬帆大鱼
相关分类