# bsearch

## PROLOG



           This manual page is part of the POSIX Programmer's Manual.  The
           Linux implementation of this interface may differ (consult the
           corresponding Linux manual page for details of Linux behavior), or
           the interface may not be implemented on Linux.
    


## NAME



           bsearch — binary search a sorted table
    


## SYNOPSIS



           #include <stdlib.h>
    
           void *bsearch(const void *_key_ , const void *_base_ , size_t _nel_ ,
               size_t _width_ , int (*_compar_)(const void *, const void *));
    


## DESCRIPTION



           The functionality described on this reference page is aligned with
           the ISO C standard. Any conflict between the requirements
           described here and the ISO C standard is unintentional. This
           volume of POSIX.1‐2017 defers to the ISO C standard.
    
           The _bsearch_() function shall search an array of _nel_ objects, the
           initial element of which is pointed to by _base_ , for an element
           that matches the object pointed to by _key_.  The size of each
           element in the array is specified by _width_.  If the _nel_ argument
           has the value zero, the comparison function pointed to by _compar_
           shall not be called and no match shall be found.
    
           The comparison function pointed to by _compar_ shall be called with
           two arguments that point to the _key_ object and to an array
           element, in that order.
    
           The application shall ensure that the comparison function pointed
           to by _compar_ does not alter the contents of the array. The
           implementation may reorder elements of the array between calls to
           the comparison function, but shall not alter the contents of any
           individual element.
    
           The implementation shall ensure that the first argument is always
           a pointer to the key.
    
           When the same objects (consisting of width bytes, irrespective of
           their current positions in the array) are passed more than once to
           the comparison function, the results shall be consistent with one
           another.  That is, the same object shall always compare the same
           way with the key.
    
           The application shall ensure that the function returns an integer
           less than, equal to, or greater than 0 if the _key_ object is
           considered, respectively, to be less than, to match, or to be
           greater than the array element. The application shall ensure that
           the array consists of all the elements that compare less than, all
           the elements that compare equal to, and all the elements that
           compare greater than the _key_ object, in that order.
    


## RETURN VALUE



           The _bsearch_() function shall return a pointer to a matching member
           of the array, or a null pointer if no match is found. If two or
           more members compare equal, which member is returned is
           unspecified.
    


## ERRORS



           No errors are defined.
    
           _The following sections are informative._
    


## EXAMPLES



           The example below searches a table containing pointers to nodes
           consisting of a string and its length. The table is ordered
           alphabetically on the string in the node pointed to by each entry.
    
           The code fragment below reads in strings and either finds the
           corresponding node and prints out the string and its length, or
           prints an error message.
    
               #include <stdio.h>
               #include <stdlib.h>
               #include <string.h>
    
               #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;
                   /* Routine to compare 2 nodes. */
                   int node_compare(const void *, const void *);
                   .
                   .
                   .
                   while (scanf("%ms", &node.string) != EOF) {
                       node_ptr = (struct node *)bsearch((void *)(&node),
                              (void *)table, TABSIZE,
                              sizeof(struct node), node_compare);
                       if (node_ptr != NULL) {
                           (void)printf("string = %20s, length = %d\n",
                               node_ptr->string, node_ptr->length);
                       } else {
                           (void)printf("not found: %s\n", node.string);
                       }
                       free(node.string);
                   }
               }
               /*
                   This routine compares two nodes based on an
                   alphabetical ordering of the string field.
               */
               int
               node_compare(const void *node1, const void *node2)
               {
                   return strcoll(((const struct node *)node1)->string,
                       ((const struct node *)node2)->string);
               }
    


## APPLICATION USAGE



           The pointers to the key and the element at the base of the table
           should be of type pointer-to-element.
    
           The comparison function need not compare every byte, so arbitrary
           data may be contained in the elements in addition to the values
           being compared.
    
           In practice, the array is usually sorted according to the
           comparison function.
    


## RATIONALE



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


## FUTURE DIRECTIONS



           None.
    


* * *