bsearch 
Syntax 
Array 
Search 
#include 
<stdlib.h> 
void 
*bsearch(key, 
base, 
nmemb, 
size, 
compar) 
void 
*key, 
*base; 
size_t 
nrnemb, 
size; 
int 
(*cornpar){); 
Defined 
in 
bsearch. 
c  in 
rts. 
src 
Description 
The  bsearch  function  searches  through 
an 
array 
of 
nmemb  objects  for  a 
member that matches the object that 
key 
points to.  Argument 
base 
points 
to 
the first member in  the array; 
size 
specifies the size  (in bytes) 
of 
each 
member. 
6-26 
The contents 
of 
the array must 
be 
in 
ascending, sorted order. 
If 
a match 
is 
found, the function returns a pointer 
to 
the matching member 
of 
the array; 
if 
no match 
is 
found, the function returns a null pointer 
(0). 
Argument 
cornpar 
points 
to 
a function that compares 
key 
to the array el-
ements.  The comparison function should 
be 
declared 
as: 
int 
cmp(ptrl, 
ptr2) 
void 
*ptrl, 
*ptr2; 
The cmp function compares the objects that 
ptrl 
and 
ptr2 
point to, and 
returns one 
of 
the 
following 
values: 
<  0 
if 
*ptrl 
is 
less 
than 
*ptr2. 
o 
if 
*ptrl 
is 
equal 
to 
*ptr2. 
>  0 
if 
*ptrl 
is 
greater than 
*ptr2.