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
11
13 bsearch — binary search a sorted table
14
16 #include <stdlib.h>
17
18 void *bsearch(const void *key, const void *base, size_t nel,
19 size_t width, int (*compar)(const void *, const void *));
20
22 The functionality described on this reference page is aligned with the
23 ISO C standard. Any conflict between the requirements described here
24 and the ISO C standard is unintentional. This volume of POSIX.1‐2008
25 defers to the ISO C standard.
26
27 The bsearch() function shall search an array of nel objects, the ini‐
28 tial element of which is pointed to by base, for an element that
29 matches the object pointed to by key. The size of each element in the
30 array is specified by width. If the nel argument has the value zero,
31 the comparison function pointed to by compar shall not be called and no
32 match shall be found.
33
34 The comparison function pointed to by compar shall be called with two
35 arguments that point to the key object and to an array element, in that
36 order.
37
38 The application shall ensure that the comparison function pointed to by
39 compar does not alter the contents of the array. The implementation may
40 reorder elements of the array between calls to the comparison function,
41 but shall not alter the contents of any individual element.
42
43 The implementation shall ensure that the first argument is always a
44 pointer to the key.
45
46 When the same objects (consisting of width bytes, irrespective of their
47 current positions in the array) are passed more than once to the com‐
48 parison function, the results shall be consistent with one another.
49 That is, the same object shall always compare the same way with the
50 key.
51
52 The application shall ensure that the function returns an integer less
53 than, equal to, or greater than 0 if the key object is considered,
54 respectively, to be less than, to match, or to be greater than the
55 array element. The application shall ensure that the array consists of
56 all the elements that compare less than, all the elements that compare
57 equal to, and all the elements that compare greater than the key
58 object, in that order.
59
61 The bsearch() function shall return a pointer to a matching member of
62 the array, or a null pointer if no match is found. If two or more mem‐
63 bers compare equal, which member is returned is unspecified.
64
66 No errors are defined.
67
68 The following sections are informative.
69
71 The example below searches a table containing pointers to nodes con‐
72 sisting of a string and its length. The table is ordered alphabetically
73 on the string in the node pointed to by each entry.
74
75 The code fragment below reads in strings and either finds the corre‐
76 sponding node and prints out the string and its length, or prints an
77 error message.
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 ((char *)p − (char *(base) % width == 0
142 (char *)p >= (char *)base
143 (char *)p < (char *)base + nel * width
144
146 None.
147
149 hcreate(), lsearch(), qsort(), tdelete()
150
151 The Base Definitions volume of POSIX.1‐2008, <stdlib.h>
152
154 Portions of this text are reprinted and reproduced in electronic form
155 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
156 -- Portable Operating System Interface (POSIX), The Open Group Base
157 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
158 cal and Electronics Engineers, Inc and The Open Group. (This is
159 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) 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.unix.org/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 2013 BSEARCH(3P)