#include <stdio.h>
#include <stdlib.h>
int comparefunc(const void * a, const void * b) {
return ( *(int*)a - *(int*)b );
}
int main () {
int arr[] = { 2, 5, 8, 10, 12, 29, 30, 40, 55, 70, 88, 99, 100, 120, 131, 160, 180 };
int size = sizeof(arr) / sizeof(arr[0]);
int key = 40;
int *item = (int*) bsearch(&key, arr, size, sizeof(int), comparefunc);
if (item != NULL) {
printf("Found item = %d\n", *item);
} else {
printf("Item = %d not found\n", *item);
}
return(0);
}
/*
run:
Found item = 40
*/