1lsearch(3C)              Standard C Library Functions              lsearch(3C)
2
3
4

NAME

6       lsearch, lfind - linear search and update
7

SYNOPSIS

9       #include <search.h>
10
11       void *lsearch(const void *key, void *base, size_t *nelp,
12            size_t width, int (*compar)(const void *, const void *));
13
14
15       void *lfind(const void *key, const void *base, size_t *nelp,
16            size_t width, int (*compar)(const void *, const void *));
17
18

DESCRIPTION

20       The  lsearch()  function  is  a  linear search routine generalized from
21       Knuth (6.1) Algorithm S. (see The Art of Computer  Programming,  Volume
22       3,  Section  6.1, by Donald E. Knuth.). It returns a pointer to a table
23       indicating where a datum can be found. If the datum does not occur,  it
24       is  added at the end of the table. The key argument points to the datum
25       to be sought in the table. The base argument points to the  first  ele‐
26       ment  in  the  table. The nelp argument points to an integer containing
27       the current number of  elements in the table.  The  integer  is  incre‐
28       mented  if  the  datum is added to the table. The width argument is the
29       size of an element in bytes. The compar argument is a  pointer  to  the
30       comparison function that the user must supply (strcmp(3C) for example).
31       It is called with two arguments that point to the elements  being  com‐
32       pared. The function must return zero if the elements are equal and non-
33       zero otherwise.
34
35
36       The lfind() function is the same as lsearch() except that if the  datum
37       is not found, it is not added to the table.  Instead, a null pointer is
38       returned.
39
40
41       It is important to note the following:
42
43           o      The pointers to the key and the element at the base  of  the
44                  table can be pointers to any type.
45
46           o      The  comparison  function  need  not  compare every byte, so
47                  arbitrary data can be contained in the elements in  addition
48                  to the values being compared.
49
50           o      The  value returned should be cast into type pointer-to-ele‐
51                  ment.
52

RETURN VALUES

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

USAGE

59       Undefined results can occur if there is not enough room in the table to
60       add a new item.
61
62
63       The  lsearch() and lfind() functions safely allows concurrent access by
64       multiple threads to disjoint data,  such  as  overlapping  subtrees  or
65       tables.
66

EXAMPLES

68       Example 1 A sample code using the lsearch() function.
69
70
71       This program will read in less than TABSIZE strings of length less than
72       ELSIZE and store them in a table, eliminating duplicates, and then will
73       print each entry.
74
75
76         #include <search.h>
77         #include <string.h>
78         #include <stdlib.h>
79         #include <stdio.h>
80
81         #define TABSIZE 50
82         #define ELSIZE 120
83
84         main()
85         {
86             char line[ELSIZE];          /* buffer to hold input string */
87             char tab[TABSIZE][ELSIZE];  /* table of strings */
88             size_t nel = 0;             /* number of entries in tab */
89             int i;
90
91             while (fgets(line, ELSIZE, stdin) != NULL &&
92                     nel < TABSIZE)
93                     (void) lsearch(line, tab, &nel, ELSIZE, mycmp);
94             for( i = 0; i < nel; i++ )
95                     (void)fputs(tab[i], stdout);
96             return 0;
97         }
98
99

ATTRIBUTES

101       See attributes(5) for descriptions of the following attributes:
102
103
104
105
106       ┌─────────────────────────────┬─────────────────────────────┐
107       │      ATTRIBUTE TYPE         │      ATTRIBUTE VALUE        │
108       ├─────────────────────────────┼─────────────────────────────┤
109       │Interface Stability          │Standard                     │
110       ├─────────────────────────────┼─────────────────────────────┤
111       │MT-Level                     │MT-Safe                      │
112       └─────────────────────────────┴─────────────────────────────┘
113

SEE ALSO

115       bsearch(3C), hsearch(3C), string(3C), tsearch(3C), attributes(5), stan‐
116       dards(5)
117
118
119       The Art of Computer Programming, Volume 3,  Sorting  and  Searching  by
120       Donald E. Knuth, published by Addison-Wesley Publishing Company, 1973.
121
122
123
124SunOS 5.11                        6 Dec 2004                       lsearch(3C)
Impressum