Switch to SpeakEasy.net DSL

The Modular Manual Browser

Home Page
Manual: (SunOS-4.1.3)
Apropos / Subsearch:
optional field

BSEARCH(3)                 Library Functions Manual                 BSEARCH(3)

       bsearch - binary search a sorted table

       #include <&lt;search.h>&gt;

       char *bsearch ((char *) key, (char *) base, nel, sizeof (*key), compar)
       unsigned nel;
       int (*compar)( );

       bsearch()  is  a  binary  search routine generalized from Knuth (6.2.1)
       Algorithm B.  It returns a pointer into  a  table  indicating  where  a
       datum  may be found.  The table must be previously sorted in increasing
       order according to a provided comparison function.   key  points  to  a
       datum  instance  to be sought in the table.  base points to the element
       at the base of the table.  nel is the number of elements in the  table.
       compar is the name of the comparison function, which is called with two
       arguments that point to the elements being compared.  The function must
       return  an integer less than, equal to, or greater than zero as accord-
       ingly the first argument is to be considered less than,  equal  to,  or
       greater than the second.

       The  example  below  searches a table containing pointers to nodes con-
       sisting of a string and its length.  The table  is  ordered  alphabeti-
       cally on the string in the node pointed to by each entry.

       This  code fragment reads in strings and either finds the corresponding
       node, in which case it prints out the string  and  its  length,  or  it
       prints an error message.
              #include <&lt;stdio.h>&gt;
              #include <&lt;search.h>&gt;
              #define   TABSIZE   1000
              struct node {            /* these are stored in the table */
                   char *string;
                   int length;
              struct node table[TABSIZE];   /* table to be searched */
                   struct node *node_ptr, node;
                   int node_compare( );  /* routine to compare 2 nodes */
                   char str_space[20];   /* space to read string into */
                   node.string = str_space;
                   while (scanf("%s", node.string) != EOF) {
                        node_ptr = (struct node *)bsearch((char *)(&&amp;node),
                                (char *)table, TABSIZE,
                                sizeof(struct node), node_compare);
                        if (node_ptr != NULL) {
                              (void)printf("string = %20s, length = %d\n",
                                  node_ptr->&gt;string, node_ptr->&gt;length);
                        } else {
                              (void)printf("not found: %s\n", node.string);
                   This routine compares two nodes based on an
                   alphabetical ordering of the string field.
              node_compare(node1, node2)
              struct node *node1, *node2;
                   return strcmp(node1->&gt;string, node2->&gt;string);

       The pointers to the key and the element at the base of the table should
       be of type pointer-to-element, and cast to type pointer-to-character.

       The comparison function need not compare every byte, so arbitrary  data
       may  be  contained in the elements in addition to the values being com-

       Although declared as  type  pointer-to-character,  the  value  returned
       should be cast into type pointer-to-element.

       hsearch(3), lsearch(3), qsort(3), tsearch(3)

       A NULL pointer is returned if the key cannot be found in the table.

                                6 October 1987                      BSEARCH(3)