Performs a binary search of a previously sorted array.
void * bsearch (void * key, void * base, size_t num, size_t width, int (*compare)(void * element1, void * element2));
Required Header |
<stdlib.h> |
Return Value
This function returns a pointer to the found key-element. A null pointer value indicates the key-element could not be found.
Parameters
key
Key representing the object to find
base
Base pointer of the array to search
num
The number of elements in the array
width
The width in bytes of each element
compare
A user written compare function to that takes two parameters:
element1, element2
pointer to the key, and an element within the array to be checked
Remarks
The bsearch function calls the user supplied function compare using the key and elements within the array until the desired key-element is found or the search is exhausted. The compare function should return the following values:
< 0 : element1 is less than element2