bsearch
bsearch is the C library's binary search. It looks for a key in a sorted array using the same style of comparator as qsort, and returns a pointer to a matching element or NULL.
Detailed Explanation
void *bsearch(const void *key, const void *base,
size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
The array at base must be ordered by compar. If it is not, the result is undefined in the useful sense: you may get NULL for a key that is present, or a pointer to the wrong element. Sort first with the same comparator.
key is a pointer to a key object, not the key bits themselves. For an array of int, you pass const int *. For an array of structs, you pass a pointer to a struct (or a dummy struct) whose compared fields are filled in. The comparator receives key as its first argument and an element address as its second on most implementations; the standard only requires that both pointers are passed, so write the comparator to treat both as const T * and not assume which is the original key.
On success you get a void * to one matching element. If several elements compare equal, which one you get is unspecified. On failure you get NULL, which is indistinguishable from "the key is present and its value is a null pointer" if you stored pointers and one of them is NULL. Store structs, or check existence another way.
bsearch does not tell you where to insert a missing key. If you need the lower bound, write the binary search yourself, or use a platform helper. The standard function is a yes-or-no plus a pointer.
nmemb == 0 returns NULL. A NULL base with a non-zero count is undefined. The array must not be modified during the search.
Code Examples
#include <stdio.h>
#include <stdlib.h>
static int cmp_int(const void *a, const void *b)
{
int x = *(const int *)a;
int y = *(const int *)b;
if (x < y) return -1;
if (x > y) return 1;
return 0;
}
int main(void)
{
int a[] = { 1, 3, 5, 7, 9 };
int key = 7;
int *found = bsearch(&key, a, 5, sizeof a[0], cmp_int);
if (found == NULL) {
printf("missing\n");
return 1;
}
printf("%d at index %td\n", *found, found - a);
/* prints: 7 at index 3 */
return 0;
}#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int cmp_str(const void *a, const void *b)
{
const char *const *ps = a;
const char *const *pt = b;
return strcmp(*ps, *pt);
}
int main(void)
{
const char *names[] = { "c", "go", "rust" }; /* already sorted */
const char *key = "go";
const char **found = bsearch(&key, names, 3, sizeof names[0], cmp_str);
printf("%s\n", found ? *found : "no"); /* prints: go */
return 0;
}Common Gotchas
Searching an unsorted array is not a slow search; it is a wrong search
sort with the same `compar` first.
`bsearch` returns one arbitrary match among equals
if you need the first, write your own lower-bound loop.
For an array of pointers, `key` is a pointer to a pointer
`bsearch(&key, ...)` where `key` is `const char *`.
A `NULL` return means "not found," not "found a NULL element"
do not store searchable keys that you cannot distinguish from failure.