1BSEARCH(P)                 POSIX Programmer's Manual                BSEARCH(P)
2
3
4

NAME

6       bsearch - binary search a sorted table
7

SYNOPSIS

9       #include <stdlib.h>
10
11       void *bsearch(const void *key, const void *base, size_t nel,
12              size_t width, int (*compar)(const void *, const void *));
13
14

DESCRIPTION

16       The  bsearch()  function shall search an array of nel objects, the ini‐
17       tial element of which is pointed  to  by  base,  for  an  element  that
18       matches  the object pointed to by key.  The size of each element in the
19       array is specified by width. If the nel argument has  the  value  zero,
20       the comparison function pointed to by compar shall not be called and no
21       match shall be found.
22
23       The comparison function pointed to by compar shall be called  with  two
24       arguments that point to the key object and to an array element, in that
25       order.
26
27       The application shall ensure that the comparison function pointed to by
28       compar  does  not  alter the contents of the array.  The implementation
29       may reorder elements of the array between calls to the comparison func‐
30       tion, but shall not alter the contents of any individual element.
31
32       The  implementation  shall  ensure  that the first argument is always a
33       pointer to the key.
34
35       When the same objects (consisting of width bytes, irrespective of their
36       current  positions  in the array) are passed more than once to the com‐
37       parison function, the results shall be  consistent  with  one  another.
38       That  is,  the  same  object shall always compare the same way with the
39       key.
40
41       The application shall ensure that the function returns an integer  less
42       than,  equal  to,  or  greater  than 0 if the key object is considered,
43       respectively, to be less than, to match, or  to  be  greater  than  the
44       array  element. The application shall ensure that the array consists of
45       all the elements that compare less than, all the elements that  compare
46       equal  to,  and  all  the  elements  that  compare greater than the key
47       object, in that order.
48

RETURN VALUE

50       The bsearch() function shall return a pointer to a matching  member  of
51       the array, or a null pointer if no match is found.  If two or more mem‐
52       bers compare equal, which member is returned is unspecified.
53

ERRORS

55       No errors are defined.
56
57       The following sections are informative.
58

EXAMPLES

60       The example below searches a table containing pointers  to  nodes  con‐
61       sisting of a string and its length. The table is ordered alphabetically
62       on the string in the node pointed to by each entry.
63
64       The code fragment below reads in strings and either  finds  the  corre‐
65       sponding  node  and  prints out the string and its length, or prints an
66       error message.
67
68
69              #include <stdio.h>
70              #include <stdlib.h>
71              #include <string.h>
72
73
74              #define TABSIZE    1000
75
76
77
78              struct node {                  /* These are stored in the table. */
79                  char *string;
80                  int length;
81              };
82              struct node table[TABSIZE];    /* Table to be searched. */
83                  .
84                  .
85                  .
86              {
87                  struct node *node_ptr, node;
88                  /* Routine to compare 2 nodes. */
89                  int node_compare(const void *, const void *);
90                  char str_space[20];   /* Space to read string into. */
91                  .
92                  .
93                  .
94                  node.string = str_space;
95                  while (scanf("%s", node.string) != EOF) {
96                      node_ptr = (struct node *)bsearch((void *)(&node),
97                             (void *)table, TABSIZE,
98                             sizeof(struct node), node_compare);
99                      if (node_ptr != NULL) {
100                          (void)printf("string = %20s, length = %d\n",
101                              node_ptr->string, node_ptr->length);
102                      } else {
103                          (void)printf("not found: %s\n", node.string);
104                      }
105                  }
106              }
107              /*
108                  This routine compares two nodes based on an
109                  alphabetical ordering of the string field.
110              */
111              int
112              node_compare(const void *node1, const void *node2)
113              {
114                  return strcoll(((const struct node *)node1)->string,
115                      ((const struct node *)node2)->string);
116              }
117

APPLICATION USAGE

119       The pointers to the key and the element at the base of the table should
120       be of type pointer-to-element.
121
122       The  comparison function need not compare every byte, so arbitrary data
123       may be contained in the elements in addition to the values  being  com‐
124       pared.
125
126       In  practice,  the  array is usually sorted according to the comparison
127       function.
128

RATIONALE

130       The requirement that the second argument (hereafter referred to  as  p)
131       to  the  comparison  function  is  a pointer to an element of the array
132       implies that for every call all of the following expressions  are  non-
133       zero:
134
135
136              ((char *)p - (char *(base) % width == 0
137              (char *)p >= (char *)base
138              (char *)p < (char *)base + nel * width
139

FUTURE DIRECTIONS

141       None.
142

SEE ALSO

144       hcreate() , lsearch() , qsort() , tsearch() , the Base Definitions vol‐
145       ume of IEEE Std 1003.1-2001, <stdlib.h>
146
148       Portions of this text are reprinted and reproduced in  electronic  form
149       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
150       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
151       Specifications  Issue  6,  Copyright  (C) 2001-2003 by the Institute of
152       Electrical and Electronics Engineers, Inc and The Open  Group.  In  the
153       event of any discrepancy between this version and the original IEEE and
154       The Open Group Standard, the original IEEE and The Open Group  Standard
155       is  the  referee document. The original Standard can be obtained online
156       at http://www.opengroup.org/unix/online.html .
157
158
159
160IEEE/The Open Group                  2003                           BSEARCH(P)
Impressum