]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/bsearch.3
Even when the CONFORMING TO section is just a list of standards,
[thirdparty/man-pages.git] / man3 / bsearch.3
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
6 .\"
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
11 .\"
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date. The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein. The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\"
23 .\" References consulted:
24 .\" Linux libc source code
25 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
26 .\" 386BSD man pages
27 .\" Modified Mon Mar 29 22:41:16 1993, David Metcalfe
28 .\" Modified Sat Jul 24 21:35:16 1993, Rik Faith (faith@cs.unc.edu)
29 .TH BSEARCH 3 2003-11-01 "" "Linux Programmer's Manual"
30 .SH NAME
31 bsearch \- binary search of a sorted array
32 .SH SYNOPSIS
33 .nf
34 .B #include <stdlib.h>
35 .sp
36 .BI "void *bsearch(const void *" key ", const void *" base ,
37 .BI " size_t " nmemb ", size_t " size ,
38 .BI " int (*" compar ")(const void *, const void *));"
39 .fi
40 .SH DESCRIPTION
41 The
42 .BR bsearch ()
43 function searches an array of \fInmemb\fP objects,
44 the initial member of which is pointed to by \fIbase\fP, for a member
45 that matches the object pointed to by \fIkey\fP.
46 The size of each member
47 of the array is specified by \fIsize\fP.
48 .PP
49 The contents of the array should be in ascending sorted order according
50 to the comparison function referenced by \fIcompar\fP.
51 The \fIcompar\fP
52 routine is expected to have two arguments which point to the \fIkey\fP
53 object and to an array member, in that order, and should return an integer
54 less than, equal to, or greater than zero if the \fIkey\fP object is found,
55 respectively, to be less than, to match, or be greater than the array
56 member.
57 .SH "RETURN VALUE"
58 The
59 .BR bsearch ()
60 function returns a pointer to a matching member of the
61 array, or NULL if no match is found.
62 If there are multiple elements that
63 match the key, the element returned is unspecified.
64 .SH "CONFORMING TO"
65 SVr4, 4.3BSD, POSIX.1-2001, C89, C99.
66 .SH EXAMPLE
67 The example below first sorts an array of structures using
68 .BR qsort (3),
69 then retrieves desired elements using
70 .BR bsearch ().
71 .sp
72 .nf
73 #include <stdio.h>
74 #include <stdlib.h>
75 #include <string.h>
76
77 struct mi {
78 int nr;
79 char *name;
80 } months[] = {
81 { 1, "jan" }, { 2, "feb" }, { 3, "mar" }, { 4, "apr" },
82 { 5, "may" }, { 6, "jun" }, { 7, "jul" }, { 8, "aug" },
83 { 9, "sep" }, {10, "oct" }, {11, "nov" }, {12, "dec" }
84 };
85
86 #define nr_of_months (sizeof(months)/sizeof(months[0]))
87
88 static int
89 compmi(const void *m1, const void *m2)
90 {
91 struct mi *mi1 = (struct mi *) m1;
92 struct mi *mi2 = (struct mi *) m2;
93 return strcmp(mi1\->name, mi2\->name);
94 }
95
96 int
97 main(int argc, char **argv)
98 {
99 int i;
100
101 qsort(months, nr_of_months, sizeof(struct mi), compmi);
102 for (i = 1; i < argc; i++) {
103 struct mi key, *res;
104 key.name = argv[i];
105 res = bsearch(&key, months, nr_of_months,
106 sizeof(struct mi), compmi);
107 if (res == NULL)
108 printf("\(aq%s\(aq: unknown month\en", argv[i]);
109 else
110 printf("%s: month #%d\en", res\->name, res\->nr);
111 }
112 exit(EXIT_SUCCESS);
113 }
114 .fi
115 .\" this example referred to in qsort.3
116 .SH "SEE ALSO"
117 .BR hsearch (3),
118 .BR lsearch (3),
119 .BR qsort (3),
120 .BR tsearch (3)