1LSEARCH(3) Linux Programmer's Manual LSEARCH(3)
2
3
4
6 lfind, lsearch - linear search of an array
7
9 #include <search.h>
10
11 void *lfind(const void *key, const void *base, size_t *nmemb,
12 size_t size, int(*compar)(const void *, const void *));
13 void *lsearch(const void *key, void *base, size_t *nmemb,
14 size_t size, int(*compar)(const void *, const void *));
15
17 lfind() and lsearch() perform a linear search for key in the array base
18 which has *nmemb elements of size bytes each. The comparison function
19 referenced by compar is expected to have two arguments which point to
20 the key object and to an array member, in that order, and which returns
21 zero if the key object matches the array member, and nonzero otherwise.
22
23 If lsearch() does not find a matching element, then the key object is
24 inserted at the end of the table, and *nmemb is incremented. In par‐
25 ticular, one should know that a matching element exists, or that more
26 room is available.
27
29 lfind() returns a pointer to a matching member of the array, or NULL if
30 no match is found. lsearch() returns a pointer to a matching member of
31 the array, or to the newly added member if no match is found.
32
34 For an explanation of the terms used in this section, see at‐
35 tributes(7).
36
37 ┌────────────────────────────────────────────┬───────────────┬─────────┐
38 │Interface │ Attribute │ Value │
39 ├────────────────────────────────────────────┼───────────────┼─────────┤
40 │lfind(), lsearch() │ Thread safety │ MT-Safe │
41 └────────────────────────────────────────────┴───────────────┴─────────┘
42
44 POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD. Present in libc since
45 libc-4.6.27.
46
48 The naming is unfortunate.
49
51 bsearch(3), hsearch(3), tsearch(3)
52
54 This page is part of release 5.13 of the Linux man-pages project. A
55 description of the project, information about reporting bugs, and the
56 latest version of this page, can be found at
57 https://www.kernel.org/doc/man-pages/.
58
59
60
61GNU 2021-03-22 LSEARCH(3)