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
20
22 The bsearch() function shall search an array of nel objects, the ini‐
23 tial element of which is pointed to by base, for an element that
24 matches the object pointed to by key. The size of each element in the
25 array is specified by width. If the nel argument has the value zero,
26 the comparison function pointed to by compar shall not be called and no
27 match shall be found.
28
29 The comparison function pointed to by compar shall be called with two
30 arguments that point to the key object and to an array element, in that
31 order.
32
33 The application shall ensure that the comparison function pointed to by
34 compar does not alter the contents of the array. The implementation
35 may reorder elements of the array between calls to the comparison func‐
36 tion, but shall not alter the contents of any individual element.
37
38 The implementation shall ensure that the first argument is always a
39 pointer to the key.
40
41 When the same objects (consisting of width bytes, irrespective of their
42 current positions in the array) are passed more than once to the com‐
43 parison function, the results shall be consistent with one another.
44 That is, the same object shall always compare the same way with the
45 key.
46
47 The application shall ensure that the function returns an integer less
48 than, equal to, or greater than 0 if the key object is considered,
49 respectively, to be less than, to match, or to be greater than the
50 array element. The application shall ensure that the array consists of
51 all the elements that compare less than, all the elements that compare
52 equal to, and all the elements that compare greater than the key
53 object, in that order.
54
56 The bsearch() function shall return a pointer to a matching member of
57 the array, or a null pointer if no match is found. If two or more mem‐
58 bers compare equal, which member is returned is unspecified.
59
61 No errors are defined.
62
63 The following sections are informative.
64
66 The example below searches a table containing pointers to nodes con‐
67 sisting of a string and its length. The table is ordered alphabetically
68 on the string in the node pointed to by each entry.
69
70 The code fragment below reads in strings and either finds the corre‐
71 sponding node and prints out the string and its length, or prints an
72 error message.
73
74
75 #include <stdio.h>
76 #include <stdlib.h>
77 #include <string.h>
78
79
80 #define TABSIZE 1000
81
82
83
84 struct node { /* These are stored in the table. */
85 char *string;
86 int length;
87 };
88 struct node table[TABSIZE]; /* Table to be searched. */
89 .
90 .
91 .
92 {
93 struct node *node_ptr, node;
94 /* Routine to compare 2 nodes. */
95 int node_compare(const void *, const void *);
96 char str_space[20]; /* Space to read string into. */
97 .
98 .
99 .
100 node.string = str_space;
101 while (scanf("%s", node.string) != EOF) {
102 node_ptr = (struct node *)bsearch((void *)(&node),
103 (void *)table, TABSIZE,
104 sizeof(struct node), node_compare);
105 if (node_ptr != NULL) {
106 (void)printf("string = %20s, length = %d\n",
107 node_ptr->string, node_ptr->length);
108 } else {
109 (void)printf("not found: %s\n", node.string);
110 }
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(), tsearch(), the Base Definitions volume
151 of IEEE Std 1003.1-2001, <stdlib.h>
152
154 Portions of this text are reprinted and reproduced in electronic form
155 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
156 -- Portable Operating System Interface (POSIX), The Open Group Base
157 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
158 Electrical and Electronics Engineers, Inc and The Open Group. In the
159 event of any discrepancy between this version and the original IEEE and
160 The Open Group Standard, the original IEEE and The Open Group Standard
161 is the referee document. The original Standard can be obtained online
162 at http://www.opengroup.org/unix/online.html .
163
164
165
166IEEE/The Open Group 2003 BSEARCH(3P)