BSEARCH - binary search through table.

(ANSI Standard)

Usage:

#include <stdlib.h>
ptr = bsearch( key, table, tabsize, keysize, compar);

Where:

const void *key;
points to the key you are searching for.
const void *table;
points to the beginning of a table that contains information to search. This table must be sorted in increasing order (according to the "compar" function -- see below).
size_t tabsize;
is the number of elements in the table.
size_t keysize;
is the size of the key you want to find.
int (*compar)(const void *,const void *);
is a pointer to a user-defined function that determines whether or not two keys are equal. This function should take two (void *) arguments; one will point to the key you want to find and the other will point to a key in the table. The function should return a negative integer if the first argument is less than the second; it should return a positive integer if the first argument is greater than the second; and it should return zero if the two arguments are equal.
void *ptr;
points to a table element that matches "key". If no match is found, a NULL pointer is returned.

Description:

"bsearch" performs a binary search through the elements of a table. It uses your "compar" function to compare elements in the table.

Note that the "compar" you use does not have to do a byte-by-byte comparison between the key and the table elements. For example, the table may be an array of C structures and "compar" may only look at a particular field when comparing two structures. In this case, the original search key might just be a dummy, with only the significant field filled in.

Copyright © 1996, Thinkage Ltd.