Everything

bsearch


Search for a value from among arranged arrays.

[Classification]

Standard library

[Syntax]

#include <stdlib.h>

void __near * __far bsearch(const void __near *key, const void __near *base, size_t nmemb, size_t size, int (__far *compar)(const void __near *, const void __near *));

void __far * __far _COM_bsearch_f(const void __far *key, const void __far *base, size_t nmemb,, size_t size, int (__far *compar)(const void __far *, const void __far *));

[Return value]

A pointer to the element in the array that coincides with key is returned. If there are two or more elements that coincide with key, the one that has been found first is indicated. If there are not elements that coincide with key, a null pointer is returned.

[Description]

This function searches an element that coincides with key from an array starting with base by means of binary search. nmemb is the number of elements of the array. size is the size of each element.

The compare function indicated by compar is called with the pointer to object key as the first parameter and the pointer to array elements as the second parameter. As a result, if the first parameter is smaller than the second parameter, a negative value is returned. If the two parameters are equal, 0 is returned. If the first parameter is greater than the second parameter, a positive value is returned. The array must be arranged in the ascending order in respect to the compare function indicated by compar (last argument).