Guardian C Library Calls Reference Manual

Reference to Library Calls
Guardian TNS C Library Calls Reference Manual128833 3-13
bsearch
bsearch
The bsearch function searches an array of elements for an element matching a specified
value.
key
points to the value to match.
base
points to the first element in the array.
nmemb
specifies the number of elements in the array.
size
specifies the size in bytes of each element.
compar(void *, void *)
points to a function that compares two elements of size size, returning an integer less
than, equal to, or greater than zero if its first argument is respectively less than,
equal to, or greater than its second argument.
Return Value
points to the matching element of the array if a match is found; otherwise, bsearch
returns the pointer value NULL.
Usage Guidelines
Before you can use bsearch, the elements of the array being searched must be sorted
in ascending order, according to the function pointed to by compar. You can sort
them using the qsort library function.
The bsearch function uses a binary, not sequential, searching algorithm.
Consequently, if two or more elements of the array are equal to *key, then bsearch
does not necessarily return the sequentially first of them as the match.
#include <stdlibh>
void *bsearch(const void *key, const void *base, size_t nmemb,
size_t size, int (*compar)(const void *, const void *));