193 lines
6.2 KiB
HTML
193 lines
6.2 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of BSEARCH</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>BSEARCH</H1>
|
|
Section: Linux Programmer's Manual (3)<BR>Updated: 2017-09-15<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
bsearch - binary search of a sorted array
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>></B>
|
|
|
|
<B>void *bsearch(const void *</B><I>key</I><B>, const void *</B><I>base</I><B>,</B>
|
|
<B> size_t </B><I>nmemb</I><B>, size_t </B><I>size</I><B>,</B>
|
|
<B> int (*</B><I>compar</I><B>)(const void *, const void *));</B>
|
|
</PRE>
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
The
|
|
<B>bsearch</B>()
|
|
|
|
function searches an array of
|
|
<I>nmemb</I>
|
|
|
|
objects,
|
|
the initial member of which is pointed to by
|
|
<I>base</I>,
|
|
|
|
for a member
|
|
that matches the object pointed to by
|
|
<I>key</I>.
|
|
|
|
The size of each member
|
|
of the array is specified by
|
|
<I>size</I>.
|
|
|
|
<P>
|
|
|
|
The contents of the array should be in ascending sorted order according
|
|
to the comparison function referenced by
|
|
<I>compar</I>.
|
|
|
|
The
|
|
<I>compar</I>
|
|
|
|
routine is expected to have two arguments which point to the
|
|
<I>key</I>
|
|
|
|
object and to an array member, in that order, and should return an integer
|
|
less than, equal to, or greater than zero if the
|
|
<I>key</I>
|
|
|
|
object is found,
|
|
respectively, to be less than, to match, or be greater than the array
|
|
member.
|
|
<A NAME="lbAE"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
The
|
|
<B>bsearch</B>()
|
|
|
|
function returns a pointer to a matching member of the
|
|
array, or NULL if no match is found.
|
|
If there are multiple elements that
|
|
match the key, the element returned is unspecified.
|
|
<A NAME="lbAF"> </A>
|
|
<H2>ATTRIBUTES</H2>
|
|
|
|
For an explanation of the terms used in this section, see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
|
|
|
|
<TABLE BORDER>
|
|
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
|
|
<TR VALIGN=top><TD>
|
|
<B>bsearch</B>()
|
|
|
|
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
|
|
</TABLE>
|
|
|
|
<P>
|
|
<A NAME="lbAG"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
|
|
<A NAME="lbAH"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
The example below first sorts an array of structures using
|
|
<B><A HREF="/cgi-bin/man/man2html?3+qsort">qsort</A></B>(3),
|
|
|
|
then retrieves desired elements using
|
|
<B>bsearch</B>().
|
|
|
|
<P>
|
|
|
|
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/string.h">string.h</A>>
|
|
<P>
|
|
struct mi {
|
|
<BR> int nr;
|
|
<BR> char *name;
|
|
} months[] = {
|
|
<BR> { 1, "jan" }, { 2, "feb" }, { 3, "mar" }, { 4, "apr" },
|
|
<BR> { 5, "may" }, { 6, "jun" }, { 7, "jul" }, { 8, "aug" },
|
|
<BR> { 9, "sep" }, {10, "oct" }, {11, "nov" }, {12, "dec" }
|
|
};
|
|
<P>
|
|
#define nr_of_months (sizeof(months)/sizeof(months[0]))
|
|
<P>
|
|
static int
|
|
compmi(const void *m1, const void *m2)
|
|
{
|
|
<BR> struct mi *mi1 = (struct mi *) m1;
|
|
<BR> struct mi *mi2 = (struct mi *) m2;
|
|
<BR> return strcmp(mi1->name, mi2->name);
|
|
}
|
|
<P>
|
|
int
|
|
main(int argc, char **argv)
|
|
{
|
|
<BR> int i;
|
|
<P>
|
|
<BR> qsort(months, nr_of_months, sizeof(struct mi), compmi);
|
|
<BR> for (i = 1; i < argc; i++) {
|
|
<BR> struct mi key, *res;
|
|
<BR> key.name = argv[i];
|
|
<BR> res = bsearch(&key, months, nr_of_months,
|
|
<BR> sizeof(struct mi), compmi);
|
|
<BR> if (res == NULL)
|
|
<BR> printf("'%s': unknown month\n", argv[i]);
|
|
<BR> else
|
|
<BR> printf("%s: month #%d\n", res->name, res->nr);
|
|
<BR> }
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
|
|
<A NAME="lbAI"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+hsearch">hsearch</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+lsearch">lsearch</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+qsort">qsort</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+tsearch">tsearch</A></B>(3)
|
|
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="1"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="2"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="3"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="4"><A HREF="#lbAE">RETURN VALUE</A><DD>
|
|
<DT id="5"><A HREF="#lbAF">ATTRIBUTES</A><DD>
|
|
<DT id="6"><A HREF="#lbAG">CONFORMING TO</A><DD>
|
|
<DT id="7"><A HREF="#lbAH">EXAMPLE</A><DD>
|
|
<DT id="8"><A HREF="#lbAI">SEE ALSO</A><DD>
|
|
<DT id="9"><A HREF="#lbAJ">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:36 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|