Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

C programming/stdlib.h/bsearch

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

In the C standard library, bsearch is a function used to search for an object in a sorted array using the binary search algorithm.http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf ISO/IEC 9899:1999 specification p. 318, § 7.20.5.1 bsearch is a generic function that can search in sorted arrays of any size, containing any kind of object, or pointer to object, and using any kind of comparison predicate. The genericity, however, comes at the expense of type-safety, since bsearch operates on void pointers; and is also expensive in the number of function calls (since each comparison requires a call to the comparison predicate), which have a large overhead. Prototype void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compare)(const void *, const void *)); // notice nmemb before size, unlike fread and fwrite Behaviour The bsearch() function returns a pointer to a matching member of the array, or NULL if no match is found. If the array has multiple matching elements the return value will be a pointer to one of those elements. Which particular element is unspecified.UNIX man pages:man 3 bsearch References External links C++ reference for std::bsearch deep