From cppreference,
To perform the search, the function performs a series of calls to compar with key as first argument and elements of the array pointed to by base as second argument.
For a given array int arr[] = {1, 2, 3, 4, 5, 6, 7}
, searching for the element 6
you need to pass a callback function to bsearch
.Does my int compar (const void* pkey, const void* pelem);
function needs to be able to return one of the following three cases:
- return < 0
- return > 0
- return 0
or it suffice to implement the compar
function just for equality (e.g return 0 when searched value equals current element)?