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 *lfind( 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 lfind 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 lfind 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:
    The lfind function returns a pointer to the array element in base that
    matches key if it is found, otherwise NULL is returned indicating that
    the key was not found.

See Also:
    bsearch, lsearch

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

    static const char *keywords[] = {
            "auto",
            "break",
            "case",
            "char",
            /* . */
            /* . */
            /* . */
            "while"
    };

    void main( int argc, const char *argv[] )
      {
        unsigned num = 5;
        int compare( const void *, const void * );

        if( argc <= 1 ) exit( EXIT_FAILURE );
        if( lfind( &argv[1], keywords, &num, sizeof(char **),
                        compare ) == NULL ) {
          printf( "'%s' is not a C keyword\n", argv[1] );
          exit( EXIT_FAILURE );
        } else {
          printf( "'%s' is a C keyword\n", argv[1] );
          exit( EXIT_SUCCESS );
        }
      }

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

Classification:
    WATCOM

Systems:
    All

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