Bsearch, Synopsis, Returns – Zilog ZUSBOPTS User Manual
Page 440: Example

Standard Functions
UM017105-0511
412
Zilog Developer Studio II – ZNEO™
User Manual
bsearch
Searches an array of nmemb objects, the initial member of which is pointed to by base, for
a member that matches the object pointed to by key. The size of each object is specified by
size.
The array has been previously sorted in ascending order according to a comparison func-
tion pointed to by
compar
, which is called with two arguments that point to the objects
being compared. The
compar
function returns an integer less than, equal to, or greater
than zero if the first argument is considered to be respectively less than, equal to, or
greater than the second.
Synopsis
#include
void *bsearch(const void *key, const void *base, size_t nmemb,
size_t size, int (*compar)(const void *, const void *));
Returns
A pointer to the matching member of the array or a null pointer, if no match is found.
Example
#include
int list[]={2,5,8,9};
int k=8;
int compare (const void * x, const void * y);
int main(void)
{
int *result;
result = bsearch(&k, list, 4, sizeof(int), compare);
}
int compare (const void * x, const void * y)
{
int a = *(int *) x;
int b = *(int *) y;
if (a < b) return -1;
if (a == b)return 0;
return 1;
}
The
compare
function prototype is, as shown in the preceding example:
int compare (const void * x, const void * y);