|
|
#include <stdlib.h>void *bsearch(const void *key, const void *buf, size_t num, size_t size, int (*compare)(const void *, const void *));
The bsearch( ) function performs a binary search on the sorted array pointed to by buf and returns a pointer to the first member that matches the key pointed to by key. The number of elements in the array is specified by num, and the size (in bytes) of each element is described by size.
The function pointed to by compare is used to compare an element of the array with the key. The form of the compare function must be as follows:
int func_name(const void *arg1, const void *arg2);
It must return values as described in the following table:
Comparison | Value Returned |
---|---|
arg1 is less than arg2 | Less than zero |
arg1 is equal to arg2 | Zero |
arg1 is greater than arg2 | Greater than zero |
The array must be sorted in ascending order with the lowest address containing the lowest element.
If the array does not contain the key, a null pointer is returned.
A related function is qsort( ).
|
|