Defined in header <stdlib.h> | ||
---|---|---|
void* bsearch( const void *key, const void *ptr, size_t count, size_t size, int (*comp)(const void*, const void*) ); | (1) | |
void* bsearch_s( const void *key, const void *ptr, rsize_t count, rsize_t size, int (*comp)(const void *, const void *, void *), void *context ); | (2) | (since C11) |
key
in an array pointed to by ptr
. The array contains count
elements of size
bytes and must be partitioned with respect to key
, that is, all the elements that compare less than must appear before all the elements that compare equal to, and those must appear before all the elements that compare greater than the key object. A fully sorted array satisfies these requirements. The elements are compared using function pointed to by comp
. The behavior is undefined if the array is not already partitioned with respect to *key
in ascending order according to the same criterion that comp
uses.context
is passed to comp
and that the following errors are detected at runtime and call the currently installed constraint handler function: count
or size
is greater than RSIZE_MAX
key
, ptr
or comp
is a null pointer (unless count
is zero) bsearch_s
is only guaranteed to be available if __STDC_LIB_EXT1__
is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__
to the integer constant 1 before including stdlib.h
.If the array contains several elements that comp
would indicate as equal to the element searched for, then it is unspecified which element the function will return as the result.
key | - | pointer to the element to search for |
ptr | - | pointer to the array to examine |
count | - | number of element in the array |
size | - | size of each element in the array in bytes |
comp | - | comparison function which returns a negative integer value if the first argument is less than the second, a positive integer value if the first argument is greater than the second and zero if the arguments are equal. int cmp(const void *a, const void *b); The function must not modify the objects passed to it and must return consistent results when called for the same objects, regardless of their positions in the array. |
context | - | additional information (e.g., collating sequence), passed to comp as the third argument |
*key
, or null pointer if such element has not been found.Despite the name, neither C nor POSIX standards require this function to be implemented using binary search or make any complexity guarantees.
Unlike other bounds-checked functions, bsearch_s
does not treat arrays of zero size as a runtime constraint violation and instead indicates element not found (the other function that accepts arrays of zero size is qsort_s
).
Until bsearch_s
, users of bsearch
often used global variables to pass additional context to the comparison function.
#include <stdlib.h> #include <stdio.h> struct data { int nr; char const *value; } dat[] = { {1, "Foo"}, {2, "Bar"}, {3, "Hello"}, {4, "World"} }; int data_cmp(void const *lhs, void const *rhs) { struct data const *const l = lhs; struct data const *const r = rhs; if (l->nr < r->nr) return -1; else if (l->nr > r->nr) return 1; else return 0; // return (l->nr > r->nr) - (l->nr < r->nr); // possible shortcut // return l->nr - r->nr; // erroneous shortcut (fails if INT_MIN is present) } int main(void) { struct data key = { .nr = 3 }; struct data const *res = bsearch(&key, dat, sizeof dat / sizeof dat[0], sizeof dat[0], data_cmp); if (res) { printf("No %d: %s\n", res->nr, res->value); } else { printf("No %d not found\n", key.nr); } }
Output:
No 3: Hello
(C11) | sorts a range of elements with unspecified type (function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/c/algorithm/bsearch