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 🫢 …or NULL if 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
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms