1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
/* bsearch example with strings */
#include <stdio.h>
#include <stdlib.h>
char strvalues[][20] = {"some","example","strings","here"};
int main ()
{
char * pItem;
char key[20] = "example";
/* sort elements in array: */
qsort (strvalues, 4, 20, (int(*)(const void*,const void*)) strcmp);
/* search for the key: */
pItem = (char*) bsearch (key, strvalues, 4, 20, (int(*)(const void*,const void*)) strcmp);
if (pItem!=NULL)
printf ("%s is in the array.\n",pItem);
else
printf ("%s is not in the array.\n",key);
return 0;
}
|