The bsearch() function shall search an array of nel objects,
the initial element of which is pointed to by
base, for an element that matches the object pointed to by key.
The size of each element in the array is specified by
width. If the nel argument has the value zero, the comparison
function pointed to by compar shall not be
called and no match shall be found.
The comparison function pointed to by compar shall be called
with two arguments that point to the key object and
to an array element, in that order.
The application shall ensure that the comparison function pointed
to by compar does not alter the contents of the array.
The implementation may reorder elements of the array between calls
to the comparison function, but shall not alter the contents of
any individual element.
The implementation shall ensure that the first argument is always
a pointer to the key.
When the same objects (consisting of width bytes, irrespective of
their current positions in the array) are passed more than
once to the comparison function, the results shall be consistent with
one another. That is, the same object shall always compare
the same way with the key.
The application shall ensure that the function returns an integer
less than, equal to, or greater than 0 if the key
object is considered, respectively, to be less than, to match, or
to be greater than the array element. The application shall
ensure that the array consists of all the elements that compare less
than, all the elements that compare equal to, and all the
elements that compare greater than the key object, in that order.
The bsearch() function shall return a pointer to a matching
member of the array, or a null pointer if no match is found.
If two or more members compare equal, which member is returned is
unspecified.
The example below searches a table containing pointers to nodes consisting
of a string and its length. The table is ordered
alphabetically on the string in the node pointed to by each entry.
The code fragment below reads in strings and either finds the corresponding
node and prints out the string and its length, or
prints an error message.
The requirement that the second argument (hereafter referred to as
p) to the comparison function is a pointer to an
element of the array implies that for every call all of the following
expressions are non-zero:
Portions of this text are reprinted and reproduced in electronic form
from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
-- Portable Operating System Interface (POSIX), The Open Group Base
Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
Electrical and Electronics Engineers, Inc and The Open Group. In the
event of any discrepancy between this version and the original IEEE and
The Open Group Standard, the original IEEE and The Open Group Standard
is the referee document. The original Standard can be obtained online at
http://www.opengroup.org/unix/online.html .
IEEE/The Open Group
BSEARCH (P)
2003
Generated by OpenAsthra.com from man3p/bsearch.3p using man macros.