Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C 6.0 - <b>lfind() linear search for key</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 lfind()                 Linear Search for Key

 #include   <search.h>                   Required for declarations only

 char       *lfind(key,base,num,width,(compare()) );
 char       *key;                        Search key
 char       *base;                       Pointer to base of search data
 unsigned   *num, width;                 Number and width of elements
 int        (*compare)(e1,e2);           Pointer to compare function
 const void *e1, *e2;     Elements compared in user function

    lfind() performs a linear search of a table of information. (Compare
    this with bsearch(), which performs a binary search.)  'key' is the
    item being sought.  'base' is a pointer to the base of an array of
    'num' elements, each of 'width' bytes.  'compare' points to a routine
    supplied by the user; it returns one of the following values after
    comparing two elements:

                   0           'element1' is identical to 'element2'

               not 0           'element1' and 'element2' are different

    Returns:    A pointer to the first occurrence of 'key' in the array
                pointed to by 'base'.  NULL is returned if 'key' is not
                found.

      Notes:    During the search, lfind() makes repeated calls to the
                'compare' routine.  On each call to 'compare', 'key' is
                compared with one of the elements of 'base'.

                lfind() does not require the array to be sorted (as
                bsearch() does).

                lsearch() operates similarly to lfind(), but lsearch()
                adds 'key' to the end of the list if it is not found;
                lfind() does not.

 Portability:   Not supported by ANSI standard.

   -------------------------------- Example ---------------------------------

    The following statements search an array for a value and print out an
    appropriate message.

           #include <search.h>
           #include <stdio.h>
           #include <string.h>

           int vals[] = {10,95,30,75,54,62,15,28,88,100};
           int num = 10;
           int key = 62;
           int *found;
           int cmp();

           main()
           {    found = (int *)lfind(&key,vals,&num,sizeof(int),cmp);
               if (found)
                  printf("%d already in table\n",key);
               else
                   printf("%d not found\n",key);
           }

           int cmp(n1,n2)
           int *n1;
           int *n2;
           {
               return(*n1 - *n2);
           }


See Also: bsearch() lsearch() qsort()

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