/* * vim:ts=4:sw=4:expandtab */ #include #include #include #include #include #include int sort_comp(const void *a, const void *b) { /* casting is dark magic */ char * const *A = a; char * const *B = b; return strcmp(*A,*B); } bool search_in_array(char *str, char *array[], size_t items) { char *cptr = NULL; printf (" ==> Search for '%s'\n", str); cptr = bsearch(&str, array, items, sizeof(char*), sort_comp); return (cptr==NULL) ? false : true ; } int main(void) { char *array[] = { "some", "example", "test", "data", "for", "sort", "and", "search", "duplicate", "data" }; size_t items = sizeof(array)/sizeof(char*) ; size_t i = 0; printf(" ==> raw test data\n"); for (i=0; i qsort call\n"); qsort(array, items, sizeof(char*), sort_comp); printf(" ==> sorted test data\n"); for (i=0; i positive hit\n"); } else { printf("\t-> not found\n"); } if (search_in_array("non-existing", array, items)) { printf("\t-> positive hit\n"); } else { printf("\t-> not found\n"); } return EXIT_SUCCESS; }