INtime SDK Help
bsearch, bsearch_s

Performs a binary search of a sorted array.

#include <stdlib.h>
#include <search.h>

void *bsearch (const void *key, const void *base, size_t num, size_t width,
    int (*compare) (const void *elem1, const void *elem2));
void *bsearch_s (const void *key, const void *base, size_t num, size_t width, int (*compare) (const void *elem1, const void *elem2, void *context),
void *context);

Parameters

key
Value being sought.
base
Pointer to base of array to be searched.
num
Number of elements in the array.
width
Width of elements in bytes.
compare
Pointer to a user-supplied routine that compares two array elements, elem1 and elem2, and returns a value specifying their relationship:
< 0 elem1 less than elem2
= 0 elem1 identical to elem2
> 0 elem1 greater than elem2
elem1
Pointer to key for the search.
elem2
Pointer to the array element to be compared with key.

Remarks

The function calls the compare routine one or more times during the search, passing pointers to two array elements on each call.

If the array you are searching is not in ascending sort order, besearch 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 Values

A pointer to the first occurrence of key in the array pointed to by base.
Success.
A null pointer if a match is not found.
Failure.

bsearch_s also returns the null pointer on runtime constraints violations.

Requirements

Versions Defined in Include Link to
INtime 3.0 intime/rt/include/stdlib.h stdlib.h
search.h
clib.lib

See Also

lfind, lsearch, qsort