Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Watcom C/C++ v10.0 : C library - <b>synopsis:</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
Synopsis:
    #include <search.h>
    void *lsearch( const void *key, /* object to search for */
                   const void *base,/* base of search data  */
                   unsigned *num,   /* number of elements   */
                   unsigned width,  /* width of each element*/
                   int (*compare)( const void *element1,
                                   const void *element2 ) );

Description:
    The lsearch function performs a linear search for the value key in the
    array of num elements pointed to by base.  Each element of the array is
    width bytes in size.  The argument compare is a pointer to a
    user-supplied routine that will be called by lsearch to determine the
    relationship of an array element with the key.  One of the arguments to
    the compare function will be an array element, and the other will be
    key.

    The compare function should return 0 if element1 is identical to
    element2 and non-zero if the elements are not identical.

Returns:
    If the key value is not found in the array, then it is added to the end
    of the array and the number of elements is incremented.  The lsearch
    function returns a pointer to the array element in base that matches key
    if it is found, or the newly added key if it was not found.

See Also:
    bsearch, lfind

Example:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <search.h>

    void main( int argc, const char *argv[] )
      {
        int i;
        unsigned num = 0;
        char **array = (char **)calloc( argc, sizeof(char **) );
        int compare( const void *, const void * );

        for( i = 1; i < argc; ++i ) {
          lsearch( &argv[i], array, &num, sizeof(char **),
                      compare );
        }
        for( i = 0; i < num; ++i ) {
          printf( "%s\n", array[i] );
        }
      }

    int compare( const void *op1, const void *op2 )
      {
        const char **p1 = (const char **) op1;
        const char **p2 = (const char **) op2;
        return( strcmp( *p1, *p2 ) );
      }

    /* With input: one two one three four */

    produces the following:

    one
    two
    three
    four

Classification:
    WATCOM

Systems:
    All

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson