1lsearch(3) Library Functions Manual lsearch(3)
2
3
4
6 lfind, lsearch - linear search of an array
7
9 Standard C library (libc, -lc)
10
12 #include <search.h>
13
14 void *lfind(const void key[.size], const void base[.size * .nmemb],
15 size_t *nmemb, size_t size,
16 int(*compar)(const void [.size], const void [.size]));
17 void *lsearch(const void key[.size], void base[.size * .nmemb],
18 size_t *nmemb, size_t size,
19 int(*compar)(const void [.size], const void [.size]));
20
22 lfind() and lsearch() perform a linear search for key in the array base
23 which has *nmemb elements of size bytes each. The comparison function
24 referenced by compar is expected to have two arguments which point to
25 the key object and to an array member, in that order, and which returns
26 zero if the key object matches the array member, and nonzero otherwise.
27
28 If lsearch() does not find a matching element, then the key object is
29 inserted at the end of the table, and *nmemb is incremented. In par‐
30 ticular, one should know that a matching element exists, or that more
31 room is available.
32
34 lfind() returns a pointer to a matching member of the array, or NULL if
35 no match is found. lsearch() returns a pointer to a matching member of
36 the array, or to the newly added member if no match is found.
37
39 For an explanation of the terms used in this section, see at‐
40 tributes(7).
41
42 ┌────────────────────────────────────────────┬───────────────┬─────────┐
43 │Interface │ Attribute │ Value │
44 ├────────────────────────────────────────────┼───────────────┼─────────┤
45 │lfind(), lsearch() │ Thread safety │ MT-Safe │
46 └────────────────────────────────────────────┴───────────────┴─────────┘
47
49 POSIX.1-2008.
50
52 POSIX.1-2001, SVr4, 4.3BSD. libc-4.6.27.
53
55 The naming is unfortunate.
56
58 bsearch(3), hsearch(3), tsearch(3)
59
60
61
62Linux man-pages 6.04 2023-03-30 lsearch(3)