OSDN Git Service

LDP: Update original to LDP v3.68
[linuxjm/LDP_man-pages.git] / original / man3 / bsearch.3
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
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.
7 .\"
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.
12 .\"
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
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\" References consulted:
26 .\"     Linux libc source code
27 .\"     Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
28 .\"     386BSD man pages
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  2003-11-01 "" "Linux Programmer's Manual"
32 .SH NAME
33 bsearch \- binary search of a sorted array
34 .SH SYNOPSIS
35 .nf
36 .B #include <stdlib.h>
37 .sp
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 *));"
41 .fi
42 .SH DESCRIPTION
43 The
44 .BR bsearch ()
45 function searches an array of
46 .I nmemb
47 objects,
48 the initial member of which is pointed to by
49 .IR base ,
50 for a member
51 that matches the object pointed to by
52 .IR key .
53 The size of each member
54 of the array is specified by
55 .IR size .
56 .PP
57 The contents of the array should be in ascending sorted order according
58 to the comparison function referenced by
59 .IR compar .
60 The
61 .I compar
62 routine is expected to have two arguments which point to the
63 .I key
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
66 .I key
67 object is found,
68 respectively, to be less than, to match, or be greater than the array
69 member.
70 .SH RETURN VALUE
71 The
72 .BR bsearch ()
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.
77 .SH CONFORMING TO
78 SVr4, 4.3BSD, POSIX.1-2001, C89, C99.
79 .SH EXAMPLE
80 The example below first sorts an array of structures using
81 .BR qsort (3),
82 then retrieves desired elements using
83 .BR bsearch ().
84 .sp
85 .nf
86 #include <stdio.h>
87 #include <stdlib.h>
88 #include <string.h>
89
90 struct mi {
91     int nr;
92     char *name;
93 } months[] = {
94     { 1, "jan" }, { 2, "feb" }, { 3, "mar" }, { 4, "apr" },
95     { 5, "may" }, { 6, "jun" }, { 7, "jul" }, { 8, "aug" },
96     { 9, "sep" }, {10, "oct" }, {11, "nov" }, {12, "dec" }
97 };
98
99 #define nr_of_months (sizeof(months)/sizeof(months[0]))
100
101 static int
102 compmi(const void *m1, const void *m2)
103 {
104     struct mi *mi1 = (struct mi *) m1;
105     struct mi *mi2 = (struct mi *) m2;
106     return strcmp(mi1\->name, mi2\->name);
107 }
108
109 int
110 main(int argc, char **argv)
111 {
112     int i;
113
114     qsort(months, nr_of_months, sizeof(struct mi), compmi);
115     for (i = 1; i < argc; i++) {
116         struct mi key, *res;
117         key.name = argv[i];
118         res = bsearch(&key, months, nr_of_months,
119                       sizeof(struct mi), compmi);
120         if (res == NULL)
121             printf("\(aq%s\(aq: unknown month\en", argv[i]);
122         else
123             printf("%s: month #%d\en", res\->name, res\->nr);
124     }
125     exit(EXIT_SUCCESS);
126 }
127 .fi
128 .\" this example referred to in qsort.3
129 .SH SEE ALSO
130 .BR hsearch (3),
131 .BR lsearch (3),
132 .BR qsort (3),
133 .BR tsearch (3)
134 .SH COLOPHON
135 This page is part of release 3.68 of the Linux
136 .I man-pages
137 project.
138 A description of the project,
139 information about reporting bugs,
140 and the latest version of this page,
141 can be found at
142 \%http://www.kernel.org/doc/man\-pages/.