liveBook · Manning

Bsearch

As explained in Modern C

bsearch is a C standard library function that searches a sorted array using a caller-provided comparison function.

bsearch searches a sorted array through a caller-provided comparison function. It receives the key, array base, element count, element size, and comparator; the untyped base pointer makes the interface generic. section 11.4

typedef int compare_function(void const*, void const*);

void* bsearch(void const* key, void const* base,
              size_t n, size_t size,
              compare_function* compar);

void qsort(void* base,
           size_t n, size_t size,
           compare_function* compar);


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":11}],[{\"line\":0,\"ch\":29},{\"line\":0,\"ch\":39}],[{\"line\":2,\"ch\":14},{\"line\":2,\"ch\":24}],[{\"line\":0,\"ch\":29},{\"line\":0,\"ch\":39}],[{\"line\":2,\"ch\":14},{\"line\":2,\"ch\":24}],[{\"line\":0,\"ch\":29},{\"line\":0,\"ch\":33}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":4}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":4}],[{\"line\":2,\"ch\":6},{\"line\":2,\"ch\":13}],[{\"line\":0,\"ch\":29},{\"line\":0,\"ch\":39}],[{\"line\":2,\"ch\":14},{\"line\":2,\"ch\":24}],[{\"line\":0,\"ch\":29},{\"line\":0,\"ch\":39}],[{\"line\":2,\"ch\":14},{\"line\":2,\"ch\":24}],[{\"line\":3,\"ch\":14},{\"line\":3,\"ch\":20}],[{\"line\":7,\"ch\":11},{\"line\":7,\"ch\":17}],[{\"line\":3,\"ch\":14},{\"line\":3,\"ch\":20}],[{\"line\":7,\"ch\":11},{\"line\":7,\"ch\":17}],[{\"line\":0,\"ch\":29},{\"line\":0,\"ch\":33}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":4}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":4}],[{\"line\":6,\"ch\":5},{\"line\":6,\"ch\":10}],[{\"line\":0,\"ch\":29},{\"line\":0,\"ch\":33}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":4}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":4}],[{\"line\":3,\"ch\":14},{\"line\":3,\"ch\":20}],[{\"line\":7,\"ch\":11},{\"line\":7,\"ch\":17}],[{\"line\":3,\"ch\":14},{\"line\":3,\"ch\":20}],[{\"line\":7,\"ch\":11},{\"line\":7,\"ch\":17}]]"}
!@%STYLE%@!

The declaration makes the essential contract visible: bsearch operates through void pointers and size parameters, while compar supplies the element ordering. section 11.4

A safe comparison function

A comparator for unsigned elements casts the two void pointers, then returns -1, 0, or +1 according to their ordering. Do not replace these tests with `return *A - *B`: integer subtraction can overflow when the values are far apart. section 11.4 section 11.4

How Modern C covers it

How deep it goes
Defines the interface and comparator contract, but does not explain the search algorithm’s internal steps.
Examples
Uses unsigned comparison code and contrasts it with an overflowing integer subtraction comparator.
What it assumes
Assumes pointers and introduces function pointers, void pointers, element sizes, and comparator conventions in context.
What you can do after
Apply via the C standard library by supplying correctly sized elements and a consistent comparison function.
How it connects
Connects function pointers, void pointers, comparison functions, arrays, and qsort.

Also explained in

Compare how 2 books explain bsearch →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.