第一次使用 bsearch() 我想知道有没有办法找到元素的位置或返回元素?
我有 bsearch() 工作,它返回一个指针,但我不能用它来打印元素。
void choose_food_item(){
char food[20];
int qty;
int size = sizeof (food_stuff_choices) / sizeof (*fs);
char * ptr_item;
do{
printf("\nPlease Choose Your Food Items!!! When finished enter display to view plan\n");
fflush(stdout);
gets(food); // searchkey
/* sort the elements of the array */
qsort(food_stuff_choices,size,sizeof(*fs),(int(*)(const void*,const void*)) strcmp);
/* search for the searchkey */
ptr_item = (char*)
bsearch (food,food_stuff_choices,size,sizeof(*fs),(int(*)(const void*,const void*)) strcmp);
if (ptr_item!=NULL){
//printf("%d\n",(int)*ptr_item);
printf("Please Enter Quantity\n");
fflush(stdout);
scanf("%d",&qty);
food_plan_item item = {food_stuff_choices[0], qty};
add_food_plan_item(item);
}
else
printf ("%s not found in the array.\n",food);
}while(strcmp("display",food) != 0);
}