169. Calling the bsearch() foreign function
The bsearch()
foreign function is part of the C standard library and has the following signature (https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/bsearch):
void *bsearch(
const void *key,
const void *base,
size_t num,
size_t width,
int ( __cdecl *compare ) (
const void *key, const void *datum)
);
In a nutshell, this method gets pointers to a key, a sorted array (base
), and a comparator. Its goal is to use the given comparator to perform a binary search of the given key
in the given array. More precisely, bsearch()
gets a pointer to the key
, a pointer to the array, the number of elements in the array (num
), the size of an element in bytes (width
), and the comparator as a callback function.
The callback function gets a pointer to key
and a pointer to the current element of the array to be compared with key
. It returns the result of comparing these two elements.
The bsearch()
function returns...