HP C/iX Library Reference Manual (30026-90004)

Chapter 5 117
HP C/iX Library Function Descriptions
bsearch
bsearch
Performs a binary search of a sorted array.
Syntax
#include <stdlib.h>
void *bsearch(const void *
key
, const void *
base
,
size_t
nmemb
, size_t
size
,
int (*
compar
) (const void *, const void *));
Parameters
key
A pointer to the search pattern to be found in the table.
base
A pointer to the beginning of a table of items to be searched.
nmemb
The number of elements in the array.
size
The total size, in bytes, of each element of the array.
compar
A pointer to the comparison function.
Return Values
x A pointer to an array element that matches the specified search pattern.
NULL No match found.
Description
The bsearch function searches an array of
nmemb
objects for a member that matches the
object pointed to by
key
. The size of each member of the array is specified by
size
.
The contents of the array must be sorted in ascending order according to the comparison
function pointed to by
compar
. The comparison function is called with two arguments that
point to the
key
object and to an array member, in that order.
The function must return an integer less than, equal to, or greater than zero indicating if
the first argument is to be considered less than, equal to, or greater than the second.
If two search keys in the array are equal to the specified object, the element matched is
unspecified.
See Also
hsearch(), lsearch(), qsort(), tsearch(), ANSI C 4.10.5.1, POSIX.1 8.1