Important Notice
The pages on this site contain documentation for very old MS-DOS software,
purely for historical purposes.
If you're looking for up-to-date documentation, particularly for programming,
you should not rely on the information found here, as it will be woefully
out of date.
bsearch
◄Summary► ◄Example► ◄Up► ◄Contents► ◄Index► ◄Back►
────────────────────────────────────────────────────────────────────────────
The bsearch function performs a binary search of a sorted array of
<num> elements, each of <width> bytes in size. The <base> value is
a pointer to the base of the array to be searched, and <key> is
the value being sought.
The <compare> argument is a pointer to a user-supplied routine
that compares the key with an array element and returns a value
specifying their relationship. The bsearch function calls the
<compare> routine one or more times during the search, passing
pointers to the key and an array element on each call. The routine
must compare the two, then return one of the following values:
Value Meaning
Less than 0 <key> less than <elem>
0 <key> identical to <elem>
Greater than 0 <key> greater than <elem>
If the array you are searching is not in ascending sort order,
bsearch 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.
Return Value
The bsearch function returns a pointer to the first occurrence of
<key> in the array pointed to by <base>. If <key> is not found,
the function returns NULL.
-♦-