Performs a binary search of a sorted array.
#include <stdlib.h> #include <search.h> void *bsearch (const void *key, const void *base, size_t num, size_t width, int (*compare) (const void *elem1, const void *elem2));
void *bsearch_s (const void *key, const void *base, size_t num, size_t width, int (*compare) (const void *elem1, const void *elem2, void *context),
void *context);
key
base
num
width
compare
elem1
and elem2
, and returns a value specifying their relationship:
< 0 |
elem1 less than elem2 |
= 0 |
elem1 identical to elem2 |
> 0 |
elem1 greater than elem2 |
elem1
elem2
key
. The function calls the compare routine one or more times during the search, passing pointers to two array elements on each call.
If the array you are searching is not in ascending sort order, besearch does not work properly. If the array contains duplicate records with identical keys, there is no way to predict which of the duplicate records will be located by bsearch.
base
.
bsearch_s also returns the null pointer on runtime constraints violations.
Versions | Defined in | Include | Link to |
---|---|---|---|
INtime 3.0 | intime/rt/include/stdlib.h | stdlib.h search.h |
clib.lib |