Your Browser is not longer supported
Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...
{{viewport.spaceProperty.prod}}
bsearch - Binary search algorithm
&pagelevel(4)&pagelevel
Definition | #include <stdlib.h> void *bsearch(const void *search, const void *field, size_t n, size_t elsize, int (*comp) (const void *, const void *)); The bsearch function is a binary search function. bsearch searches the number n of elements of an array field for the value in the data item search. Each array element is elsize bytes long. The array elements must already be sorted in ascending order as expected by the comparison function cmp. cmp is a user-supplied comparison function which is called by bsearch with two arguments, a pointer to search (argument 1) and a pointer to an array element (argument 2). cmp supplies an integer as the result. The result is interpreted as follows: < 0 | argument1 is less than argument2 | = 0 | argument1 and argument2 are equal | > 0 | argument1 is greater than argument2 |
|
Return val. | Pointer to the array element found. |
|
| If more than one instance of the element is found there is no indication as to which element the pointer refers to. |
| NULL pointer | if no element has been found. |
Note | If, for example, the qsort function is used for sorting the array, it makes sense to use the same comparison function cmp that is used by bsearch . The current arguments of qsort are then pointers to two array elements to be compared. |
See also | qsort |
|