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 2020-11-01 "" "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
86 Interface Attribute Value
89 T} Thread safety MT-Safe
95 POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
97 The example below first sorts an array of structures using
99 then retrieves desired elements using
111 { 1, "jan" }, { 2, "feb" }, { 3, "mar" }, { 4, "apr" },
112 { 5, "may" }, { 6, "jun" }, { 7, "jul" }, { 8, "aug" },
113 { 9, "sep" }, {10, "oct" }, {11, "nov" }, {12, "dec" }
116 #define nr_of_months (sizeof(months)/sizeof(months[0]))
119 compmi(const void *m1, const void *m2)
121 const struct mi *mi1 = m1;
122 const struct mi *mi2 = m2;
123 return strcmp(mi1\->name, mi2\->name);
127 main(int argc, char **argv)
129 qsort(months, nr_of_months, sizeof(months[0]), compmi);
130 for (int i = 1; i < argc; i++) {
135 res = bsearch(&key, months, nr_of_months,
136 sizeof(months[0]), compmi);
138 printf("\(aq%s\(aq: unknown month\en", argv[i]);
140 printf("%s: month #%d\en", res\->name, res\->nr);
145 .\" this example referred to in qsort.3