, , , bsearch, , . NULL, , , , , , , .
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int pstrcmp(const void *a, const void *b)
{
return strcmp(*(char * const *)a, *(char * const *)b);
}
static char *check_for_match(char *input)
{
static char *static_list[] = { "one", "two", "three", "four", "five" };
static int nelems;
if (! nelems)
{
nelems = sizeof(static_list) / sizeof(*static_list);
qsort(static_list, nelems, sizeof(*static_list), pstrcmp);
}
return bsearch(&input, static_list, nelems, sizeof(*static_list), pstrcmp);
}
int main(int argc, char *argv[])
{
if (check_for_match("should_not_match"))
{
printf("Match found.\n");
} else {
printf("No match found.\n");
}
if (check_for_match("two"))
{
printf("Match found.\n");
} else {
printf("No match found.\n");
}
return EXIT_SUCCESS;
}