Home > Electronics Tutorials > C Language Tutorial > stdlib.h - Searching and Sorting Functions - bsearch

C Language Programming Library Reference Guide

tdlib.h - Searching and Sorting Functions - bsearch

Declaration: void *bsearch(const void *key, const void *base, size_t nitems, size_t size, int (*compar)(const void *, const void *)); Performs a binary search. The beginning of the array is pointed to by base. It searches for an element equal to that pointed to by key. The array is nitems long with each element in the array size bytes long.

The method of comparing is specified by the compar function. This function takes two arguments, the first is the key pointer and the second is the current element in the array being compared. This function must return less than zero if the compared value is less than the specified key. It must return zero if the compared value is equal to the specified key. It must return greater than zero if the compared value is greater than the specified key.

The array must be arranged so that elements that compare less than key are first, elements that equal key are next, and elements that are greater than key are last.

If a match is found, a pointer to this match is returned. Otherwise a null pointer is returned. If multiple matching keys are found, which key is returned is unspecified

Note: To report broken links or to submit your projects, tutorials please email to Webmaster

Discover

     more......