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