Everything

bsearch


Performs binary search.

[Format]

// C

#include <stdlib.h>

void *bsearch (const void *key, const void *base, size_t nmemb, size_t size, long (*compar)(const void *, const void *));

 

// C++/EC++

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

[Parameters]

key Pointer to data to find

base Pointer to a table to be searched

nmemb Number of members to be searched

size Number of bytes of a member to be searched

compar Pointer to a function that performs comparison

[Return values]

If a matching member is found: Pointer to the matching member

If no matching member is found: NULL

[Remarks]

The bsearch function searches the table specified by base for a member that matches the data specified by key, by binary search method. The function that performs comparison should receive pointers p1 (first parameter) and p2 (second parameter) to two data items to compare, and return the result complying with the specification below.

*p1 < *p2: Returns a negative value.

*p1 == *p2: Returns 0.

*p1 > *p2: Returns a positive value.

Members to be searched must be placed in the ascending order.