How to Use bsearch in C: Syntax and Example
In C,
bsearch performs a binary search on a sorted array to find an element quickly. You provide the key, the array, the number of elements, the size of each element, and a comparison function. It returns a pointer to the found element or NULL if not found.Syntax
The bsearch function is declared in <stdlib.h> and has this syntax:
void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
Explanation:
key: Pointer to the item you want to find.base: Pointer to the first element of the sorted array.nmemb: Number of elements in the array.size: Size in bytes of each element.compar: Pointer to a function that compares two elements and returns an integer.
c
void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
Example
This example shows how to search for an integer in a sorted array using bsearch. The comparison function returns negative, zero, or positive depending on the order.
c
#include <stdio.h> #include <stdlib.h> int compare_ints(const void *a, const void *b) { int arg1 = *(const int *)a; int arg2 = *(const int *)b; return (arg1 > arg2) - (arg1 < arg2); } int main() { int values[] = {1, 3, 5, 7, 9, 11}; int key = 7; int *item = (int *)bsearch(&key, values, 6, sizeof(int), compare_ints); if (item != NULL) { printf("Found %d at index %ld\n", *item, item - values); } else { printf("%d not found\n", key); } return 0; }
Output
Found 7 at index 3
Common Pitfalls
Common mistakes when using bsearch include:
- Not sorting the array before calling
bsearch. The array must be sorted according to the comparison function. - Using a wrong comparison function that does not follow the expected contract (return negative, zero, or positive).
- Passing incorrect sizes or counts, causing out-of-bounds access.
- Misinterpreting the returned pointer; it points to the found element, not an index.
c
#include <stdio.h> #include <stdlib.h> int compare_ints(const void *a, const void *b) { int arg1 = *(const int *)a; int arg2 = *(const int *)b; return (arg1 > arg2) - (arg1 < arg2); } // Wrong: array not sorted int main() { int values[] = {3, 1, 7, 5, 9}; // Not sorted int key = 5; int *item = (int *)bsearch(&key, values, 5, sizeof(int), compare_ints); if (item != NULL) { printf("Found %d\n", *item); } else { printf("Not found\n"); } return 0; } // Correct: sort first // qsort(values, 5, sizeof(int), compare_ints);
Output
Not found
Quick Reference
| Parameter | Description |
|---|---|
| key | Pointer to the element to search for |
| base | Pointer to the first element of the sorted array |
| nmemb | Number of elements in the array |
| size | Size in bytes of each element |
| compar | Comparison function pointer returning negative, zero, or positive |
Key Takeaways
Always ensure the array is sorted before using bsearch.
Provide a correct comparison function that returns negative, zero, or positive values.
bsearch returns a pointer to the found element or NULL if not found.
Use the correct element size and count to avoid errors.
The returned pointer can be used to find the index by pointer arithmetic.