1BSEARCH(3P) POSIX Programmer's Manual BSEARCH(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 bsearch — binary search a sorted table
13
15 #include <stdlib.h>
16
17 void *bsearch(const void *key, const void *base, size_t nel,
18 size_t width, int (*compar)(const void *, const void *));
19
21 The functionality described on this reference page is aligned with the
22 ISO C standard. Any conflict between the requirements described here
23 and the ISO C standard is unintentional. This volume of POSIX.1‐2017
24 defers to the ISO C standard.
25
26 The bsearch() function shall search an array of nel objects, the ini‐
27 tial element of which is pointed to by base, for an element that
28 matches the object pointed to by key. The size of each element in the
29 array is specified by width. If the nel argument has the value zero,
30 the comparison function pointed to by compar shall not be called and no
31 match shall be found.
32
33 The comparison function pointed to by compar shall be called with two
34 arguments that point to the key object and to an array element, in that
35 order.
36
37 The application shall ensure that the comparison function pointed to by
38 compar does not alter the contents of the array. The implementation may
39 reorder elements of the array between calls to the comparison function,
40 but shall not alter the contents of any individual element.
41
42 The implementation shall ensure that the first argument is always a
43 pointer to the key.
44
45 When the same objects (consisting of width bytes, irrespective of their
46 current positions in the array) are passed more than once to the com‐
47 parison function, the results shall be consistent with one another.
48 That is, the same object shall always compare the same way with the
49 key.
50
51 The application shall ensure that the function returns an integer less
52 than, equal to, or greater than 0 if the key object is considered,
53 respectively, to be less than, to match, or to be greater than the
54 array element. The application shall ensure that the array consists of
55 all the elements that compare less than, all the elements that compare
56 equal to, and all the elements that compare greater than the key
57 object, in that order.
58
60 The bsearch() function shall return a pointer to a matching member of
61 the array, or a null pointer if no match is found. If two or more mem‐
62 bers compare equal, which member is returned is unspecified.
63
65 No errors are defined.
66
67 The following sections are informative.
68
70 The example below searches a table containing pointers to nodes con‐
71 sisting of a string and its length. The table is ordered alphabetically
72 on the string in the node pointed to by each entry.
73
74 The code fragment below reads in strings and either finds the corre‐
75 sponding node and prints out the string and its length, or prints an
76 error message.
77
78
79 #include <stdio.h>
80 #include <stdlib.h>
81 #include <string.h>
82
83 #define TABSIZE 1000
84
85 struct node { /* These are stored in the table. */
86 char *string;
87 int length;
88 };
89 struct node table[TABSIZE]; /* Table to be searched. */
90 .
91 .
92 .
93 {
94 struct node *node_ptr, node;
95 /* Routine to compare 2 nodes. */
96 int node_compare(const void *, const void *);
97 .
98 .
99 .
100 while (scanf("%ms", &node.string) != EOF) {
101 node_ptr = (struct node *)bsearch((void *)(&node),
102 (void *)table, TABSIZE,
103 sizeof(struct node), node_compare);
104 if (node_ptr != NULL) {
105 (void)printf("string = %20s, length = %d\n",
106 node_ptr->string, node_ptr->length);
107 } else {
108 (void)printf("not found: %s\n", node.string);
109 }
110 free(node.string);
111 }
112 }
113 /*
114 This routine compares two nodes based on an
115 alphabetical ordering of the string field.
116 */
117 int
118 node_compare(const void *node1, const void *node2)
119 {
120 return strcoll(((const struct node *)node1)->string,
121 ((const struct node *)node2)->string);
122 }
123
125 The pointers to the key and the element at the base of the table should
126 be of type pointer-to-element.
127
128 The comparison function need not compare every byte, so arbitrary data
129 may be contained in the elements in addition to the values being com‐
130 pared.
131
132 In practice, the array is usually sorted according to the comparison
133 function.
134
136 The requirement that the second argument (hereafter referred to as p)
137 to the comparison function is a pointer to an element of the array
138 implies that for every call all of the following expressions are non-
139 zero:
140
141
142 ( (char *)p - (char *)base ) % width == 0
143 (char *)p >= (char *)base
144 (char *)p < (char *)base + nel * width
145
147 None.
148
150 hcreate(), lsearch(), qsort(), tdelete()
151
152 The Base Definitions volume of POSIX.1‐2017, <stdlib.h>
153
155 Portions of this text are reprinted and reproduced in electronic form
156 from IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
157 table Operating System Interface (POSIX), The Open Group Base Specifi‐
158 cations Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of
159 Electrical and Electronics Engineers, Inc and The Open Group. In the
160 event of any discrepancy between this version and the original IEEE and
161 The Open Group Standard, the original IEEE and The Open Group Standard
162 is the referee document. The original Standard can be obtained online
163 at http://www.opengroup.org/unix/online.html .
164
165 Any typographical or formatting errors that appear in this page are
166 most likely to have been introduced during the conversion of the source
167 files to man page format. To report such errors, see https://www.ker‐
168 nel.org/doc/man-pages/reporting_bugs.html .
169
170
171
172IEEE/The Open Group 2017 BSEARCH(3P)