bsearch

Binary search

#include <stdlib.h>
void *bsearch (
        const void *key,
        const void *base,
        size_t n,
        size_t w,
        long(*fcmp)(const void *, const void *)
)

Arguments

key Pointer to data which is the retrieval key
base Pointer to destination area to be searched
n Number of elements in the data area
w Size of 1 element
fcmp Pointer to comparison function

Return Value

The address of the first item matching the retrieval key is returned. 0 is returned if a matching item was not found.

Explanation

A binary search for the retrieval key pointed to by key is made in the destination area pointed to by base using the comparison function fcmp. A pointer to the found item is returned. base is considered to be an array of n items with size w. The search is performed looking for the first item that matches the retrieval key. fcmp must return a value representing the result of the comparison as in bcmp().