Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Borland C++ 2.x ( with Turbo C ) - <b>lsearch() linear search for key; add key if not found</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 lsearch()               Linear Search for Key; Add Key If Not Found

 #include   <stdlib.h>

 void       *lsearch(key,base,num,width,compare);
 const void *key;                        Search key
 void       *base;                       Pointer to base of search data
 size_t     *num;                        Pointer to number of elements
 size_t     width;                       Width of elements
 int (*compare)(const void *e1, const void *e2);  Compare function

    lsearch() performs a linear search of a table of information.
    (Compare this with bsearch(), which performs a binary search.)  'num'
    points to the number of elements in the table, each element being
    'width' in size;  'base' is a pointer to the base of the array to be
    searched; and 'key' is the item being sought.  'compare' points to a
    user-supplied routine that compares two elements and returns a value
    based on the comparison. 'compare' returns one of the following
    values:

                   0           'e1' is identical to 'e2'

               not 0           'e1' and 'e2' 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, and lsearch() adds 'key' to the
                    end of the list. (Compare with lfind(), which
                    operates like lsearch() but does not add 'key' to the
                    end of the list.)

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

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

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

    The following statements search an array for a value.  If the value
    is not found, it is added to the end of the array.

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

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

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

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


See Also: bsearch() lfind() qsort()

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