bsearch

Performs a binary search of a sorted array.

Library:LibC
Classification:ANSI
Service:General C Services

Syntax

  #include <stdlib.h> 
   
  void *bsearch (
     const void   *key,   
     const void   *base,   
     size_t        num,   
     size_t        width,   
     int           (*compar) (
                       const void   *, 
                       const void   *));
  

Parameters

key

(IN) Points to the string containing the characters to be matched.

base

(IN) Points to the sorted array to search.

num

(IN) Specifies the number of elements in the array.

width

(IN) Specifies the size (in bytes) of each element in the array.

compar

(IN/OUT) Points to the comparison function.

Return Values

Returns a pointer to the matching member of the array, or a NULL pointer if a matching object could not be found.

Remarks

The bsearch function performs a binary search of a sorted array of elements (pointed to by the base parameter), for an item that matches the object pointed to by the key parameter.

The comparison function pointed to by the compar parameter is called with two arguments that point to elements in the array. The comparison function returns an integer less than, equal to, or greater than zero if the first argument is less than, equal to, or greater than the second parameter.

See Also

qsort