Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

bsearch(3)

hsearch(3)

string(3)

tsearch(3)



  lsearch(3)                          CLIX                          lsearch(3)



  NAME

    lsearch, lfind - Performs linear search and update

  LIBRARY

    The Standard C Library (libc.a)

  SYNOPSIS

    #include <stdio.h>

    #include <search.h>

    char *lsearch(
      char *key ,
      char *base ,
      int *nelp ,
      unsigned *width ,
      int *compar() );

    char *lfind(
      char *key ,
      char *base ,
      int *nelp ,
      unsigned width ,
      int *compar() );

  PARAMETERS

    key      A pointer to the datum being sought

    base     A pointer to the first element in the lookup table

    nelp     A pointer an integer containing the current number of elements in
             the lookup table

    compar   A comparison function

  DESCRIPTION

    The lsearch() function is a linear search function generalized from Knuth
    (6.1) Algorithm S.  It returns a pointer into a table indicating where a
    datum may be found.  If the datum does not occur, it is added at the end
    of the table.  The value of nelp is incremented if the datum is added to
    the table.  The compar() function is the name of the comparison function
    which the user must supply [for example: strcmp()].  It is called with two
    arguments that point to the elements being compared.  The function must
    return 0 if the elements are equal and nonzero otherwise.

    The lfind() function is the same as lsearch() except that if the datum is



  2/94 - Intergraph Corporation                                              1






  lsearch(3)                          CLIX                          lsearch(3)



    not found, it is not added to the table.  Instead, a pointer is returned.

  EXAMPLES

    The following program reads in less than TABSIZE strings of length less
    than ELSIZE and stores them in a table, eliminating duplicates, and
    displays the strings in the table:

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

    #define TABSIZE 5 /*The size of the table.*/
    #define ELSIZE 80 /*The length of the string.*/

    main()
    {
         char line[ELSIZE];
         char tab[TABSIZE][ELSIZE]
         char *lsearch();

         unsigned nel = 0;
         int strcmp(), i;

         while(nel < TABSIZE) {
              fgets(line, ELSIZE, stdin);
              (void)lsearch(line, (char *)tab, &nel, ELSIZE, strcmp);
         }
         for(i = 0; i < TABSIZE; i++) {
              printf("str %d is %s", i, tab[i]);
         }
    }


  NOTES

    The pointers to the key and the element at the base of the table should be
    of type pointer-to-element, and cast to type pointer-to-character.

    The comparison function need not compare every byte, so arbitrary data may
    be contained in the elements in addition to the values being compared.

    Although declared as type pointer-to-character, the value returned should
    be cast into type pointer-to-element.

  CAUTIONS

    Undefined results can occur if there is not enough room in the table to
    add a new item.

  RETURN VALUES




  2                                              Intergraph Corporation - 2/94






  lsearch(3)                          CLIX                          lsearch(3)



    If the datum searched for is found, both lsearch() and find() return a
    pointer to it.  Otherwise, lfind() returns NULL, and lsearch() returns a
    pointer to the newly added element.

  RELATED INFORMATION

    Functions:  bsearch(3), hsearch(3), string(3), tsearch(3)















































  2/94 - Intergraph Corporation                                              3




Typewritten Software • bear@typewritten.org • Edmonds, WA 98026