1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" References consulted:
26 .\" Linux libc source code
27 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
29 .\" Modified Mon Mar 29 22:41:16 1993, David Metcalfe
30 .\" Modified Sat Jul 24 21:35:16 1993, Rik Faith (faith@cs.unc.edu)
31 .TH BSEARCH 3 2017-09-15 "" "Linux Programmer's Manual"
33 bsearch \- binary search of a sorted array
36 .B #include <stdlib.h>
38 .BI "void *bsearch(const void *" key ", const void *" base ,
39 .BI " size_t " nmemb ", size_t " size ,
40 .BI " int (*" compar ")(const void *, const void *));"
45 function searches an array of
48 the initial member of which is pointed to by
51 that matches the object pointed to by
53 The size of each member
54 of the array is specified by
57 The contents of the array should be in ascending sorted order according
58 to the comparison function referenced by
62 routine is expected to have two arguments which point to the
64 object and to an array member, in that order, and should return an integer
65 less than, equal to, or greater than zero if the
68 respectively, to be less than, to match, or be greater than the array
73 function returns a pointer to a matching member of the
74 array, or NULL if no match is found.
75 If there are multiple elements that
76 match the key, the element returned is unspecified.
78 For an explanation of the terms used in this section, see
84 Interface Attribute Value
87 T} Thread safety MT-Safe
91 POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
93 The example below first sorts an array of structures using
95 then retrieves desired elements using
107 { 1, "jan" }, { 2, "feb" }, { 3, "mar" }, { 4, "apr" },
108 { 5, "may" }, { 6, "jun" }, { 7, "jul" }, { 8, "aug" },
109 { 9, "sep" }, {10, "oct" }, {11, "nov" }, {12, "dec" }
112 #define nr_of_months (sizeof(months)/sizeof(months[0]))
115 compmi(const void *m1, const void *m2)
117 struct mi *mi1 = (struct mi *) m1;
118 struct mi *mi2 = (struct mi *) m2;
119 return strcmp(mi1\->name, mi2\->name);
123 main(int argc, char **argv)
127 qsort(months, nr_of_months, sizeof(struct mi), compmi);
128 for (i = 1; i < argc; i++) {
131 res = bsearch(&key, months, nr_of_months,
132 sizeof(struct mi), compmi);
134 printf("\(aq%s\(aq: unknown month\en", argv[i]);
136 printf("%s: month #%d\en", res\->name, res\->nr);
141 .\" this example referred to in qsort.3