lsearch(3)
π lsearch(3) β like, the linear search one π
okay bestie, pull up a chair πͺ because we're talking about lsearch(3) and lfind(3) from the Library Functions Manual β that's the libc crew, and today's topic is linear search of an array. no cap. β¨
πΈ NAME πΈ
lfind, lsearch β linear search of an array π¦
π LIBRARY π
Standard C library β that's libc, aka -lc. she's everywhere, she's iconic, you already linked her π
β¨ SYNOPSIS β¨
#include <search.h>
void *lfind(size_t *n, size_t size;
const void key[size],
const void base[*n * size],
size_t *n, size_t size,
typeof(int (const void [size], const void [size]))
*compar);
void *lsearch(size_t *n, size_t size;
const void key[size],
void base[*n * size],
size_t *n, size_t size,
typeof(int (const void [size], const void [size]))
*compar);
copy it character for character, babe, don't you dare "clean it up" π
π DESCRIPTION π
so lfind() and lsearch() do a linear search for key in the array base. that array's got *n elements, and every single one of them is size bytes. one by one, no skipping, she's thorough like that π§
the comparison function that compar points to? it's expected to take two arguments β one pointing at the key object, the other pointing at an array member, in that order. it returns zero if the key object matches the array member, and nonzero otherwise. that's the whole vibe check. β
now here's the plot twist π β if lsearch() does not find a matching element, the key object gets inserted at the end of the table, and *n gets incremented. so basically:
β οΈ bestie listen β you need to know that a matching element exists, or that there's more room available. don't just rawdog it into a full array, that's not the move. π
π« RETURN VALUE π«
lfind()hands you back a pointer to a matching member of the array π«Ά β¦orNULLif no match was found. sad girl hours.lsearch()hands you back a pointer to a matching member of the array, or to the newly added member if no match was found. she always delivers something, queen behaviour. π
π ATTRIBUTES π
for the full explanation of the terms used in this section, go peek at attributes(7). π
- Interface:
lfind(),lsearch() - Attribute: Thread safety
- Value:
MT-Safe
MT-Safe!! go off, thread-safe icons π₯
π STANDARDS π
POSIX.1-2008. certified. periodt. π
π°οΈ HISTORY π°οΈ
POSIX.1-2001, SVr4, 4.3BSD. and libc-4.6.27. she's been around longer than you've been alive, respect your elders π
π BUGS π
literally just this:
The naming is unfortunate.
that's it. that's the bug note. the naming is unfortunate ππ honestly? real. lfind vs lsearch is giving "i named my two cats Cat and Not-Cat" energy. not me thinking about this for the rest of the day. π
π SEE ALSO π
okay that's the whole page, bestie!! go forth and linearly search. you're doing amazing sweetie π«Άπ
this was Linux man-pages 6.20, dated 2026-02-08, and yes, it's the lsearch(3) page β k byeeee π¦β¨π
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: Linux-man-pages-copyleft
- Copyright 1995, James R. Van Zandt <jrv@vanzandt.mv.com>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms