BSEARCH(3)                 POSIX Programmer's Manual                BSEARCH(3)



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


NNAAMMEE
       bsearch -- binary search a sorted table

SSYYNNOOPPSSIISS
       #include <stdlib.h>

       void *bsearch(const void *_k_e_y, const void *_b_a_s_e, size_t _n_e_l,
           size_t _w_i_d_t_h, int (*_c_o_m_p_a_r)(const void *, const void *));

DDEESSCCRRIIPPTTIIOONN
       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-2008
       defers to the ISO C standard.

       The _b_s_e_a_r_c_h() function shall search an array of _n_e_l objects, the
       initial element of which is pointed to by _b_a_s_e, for an element that
       matches the object pointed to by _k_e_y.  The size of each element in the
       array is specified by _w_i_d_t_h.  If the _n_e_l argument has the value zero,
       the comparison function pointed to by _c_o_m_p_a_r shall not be called and no
       match shall be found.

       The comparison function pointed to by _c_o_m_p_a_r shall be called with two
       arguments that point to the _k_e_y object and to an array element, in that
       order.

       The application shall ensure that the comparison function pointed to by
       _c_o_m_p_a_r 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 _k_e_y 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 _k_e_y
       object, in that order.

RREETTUURRNN VVAALLUUEE
       The _b_s_e_a_r_c_h() 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.

EERRRROORRSS
       No errors are defined.

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

EEXXAAMMPPLLEESS
       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.

           ##iinncclluuddee <<ssttddiioo..hh>>
           ##iinncclluuddee <<ssttddlliibb..hh>>
           ##iinncclluuddee <<ssttrriinngg..hh>>

           #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);
           }

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       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.

RRAATTIIOONNAALLEE
       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:

           ((((cchhaarr **))pp -- ((cchhaarr **((bbaassee)) %% wwiiddtthh ==== 00
           ((cchhaarr **))pp >>== ((cchhaarr **))bbaassee
           ((cchhaarr **))pp << ((cchhaarr **))bbaassee ++ nneell ** wwiiddtthh

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _h_c_r_e_a_t_e(), _l_s_e_a_r_c_h(), _q_s_o_r_t(), _t_d_e_l_e_t_e()

       The Base Definitions volume of POSIX.1-2008, <<ssttddlliibb..hh>>

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
       -- Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, Copyright (C) 2013 by the Institute of
       Electrical and Electronics Engineers, Inc and The Open Group.  (This is
       POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
       event of any discrepancy between this version and the original IEEE and
       The Open Group Standard, the original IEEE and The Open Group Standard
       is the referee document. The original Standard can be obtained online
       at http://www.unix.org/online.html .

       Any typographical or formatting errors that appear in this page are
       most likely to have been introduced during the conversion of the source
       files to man page format. To report such errors, see
       https://www.kernel.org/doc/man-pages/reporting_bugs.html .



IEEE/The Open Group                  2013                           BSEARCH(3)
