| 1 |
'\" t |
| 2 |
.\" Copyright 1995 Jim Van Zandt <[email protected]> |
| 3 |
.\" |
| 4 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 5 |
.\" |
| 6 |
.\" Corrected prototype and include, aeb, 990927 |
| 7 |
.TH lsearch 3 2024-05-02 "Linux man-pages 6.9.1" |
| 8 |
.SH NAME |
| 9 |
lfind, lsearch \- linear search of an array |
| 10 |
.SH LIBRARY |
| 11 |
Standard C library |
| 12 |
.RI ( libc ", " \-lc ) |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
.B #include <search.h> |
| 16 |
.P |
| 17 |
.BI "void *lfind(const void " key [. size "], \ |
| 18 |
const void " base [. size " * ." nmemb ], |
| 19 |
.BI " size_t *" nmemb ", size_t " size , |
| 20 |
.BI " int(*" compar ")(const void [." size "], \ |
| 21 |
const void [." size ])); |
| 22 |
.BI "void *lsearch(const void " key [. size "], \ |
| 23 |
void " base [. size " * ." nmemb ], |
| 24 |
.BI " size_t *" nmemb ", size_t " size , |
| 25 |
.BI " int(*" compar ")(const void [." size "], \ |
| 26 |
const void [." size ])); |
| 27 |
.fi |
| 28 |
.SH DESCRIPTION |
| 29 |
.BR lfind () |
| 30 |
and |
| 31 |
.BR lsearch () |
| 32 |
perform a linear search for |
| 33 |
.I key |
| 34 |
in the array |
| 35 |
.I base |
| 36 |
which has |
| 37 |
.I *nmemb |
| 38 |
elements of |
| 39 |
.I size |
| 40 |
bytes each. |
| 41 |
The comparison function referenced by |
| 42 |
.I compar |
| 43 |
is expected to have two arguments which point to the |
| 44 |
.I key |
| 45 |
object and to an array member, in that order, and which |
| 46 |
returns zero if the |
| 47 |
.I key |
| 48 |
object matches the array member, and |
| 49 |
nonzero otherwise. |
| 50 |
.P |
| 51 |
If |
| 52 |
.BR lsearch () |
| 53 |
does not find a matching element, then the |
| 54 |
.I key |
| 55 |
object is inserted at the end of the table, and |
| 56 |
.I *nmemb |
| 57 |
is |
| 58 |
incremented. |
| 59 |
In particular, one should know that a matching element |
| 60 |
exists, or that more room is available. |
| 61 |
.SH RETURN VALUE |
| 62 |
.BR lfind () |
| 63 |
returns a pointer to a matching member of the array, or |
| 64 |
NULL if no match is found. |
| 65 |
.BR lsearch () |
| 66 |
returns a pointer to |
| 67 |
a matching member of the array, or to the newly added member if no |
| 68 |
match is found. |
| 69 |
.SH ATTRIBUTES |
| 70 |
For an explanation of the terms used in this section, see |
| 71 |
.BR attributes (7). |
| 72 |
.TS |
| 73 |
allbox; |
| 74 |
lbx lb lb |
| 75 |
l l l. |
| 76 |
Interface Attribute Value |
| 77 |
T{ |
| 78 |
.na |
| 79 |
.nh |
| 80 |
.BR lfind (), |
| 81 |
.BR lsearch () |
| 82 |
T} Thread safety MT-Safe |
| 83 |
.TE |
| 84 |
.SH STANDARDS |
| 85 |
POSIX.1-2008. |
| 86 |
.SH HISTORY |
| 87 |
POSIX.1-2001, SVr4, 4.3BSD. |
| 88 |
libc-4.6.27. |
| 89 |
.SH BUGS |
| 90 |
The naming is unfortunate. |
| 91 |
.SH SEE ALSO |
| 92 |
.BR bsearch (3), |
| 93 |
.BR hsearch (3), |
| 94 |
.BR tsearch (3) |