Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

lsearch, lfind - linear search and update

&pagelevel(4)&pagelevel

Syntax

#include <search.h>

void *lsearch (const void *key, void * base, size_t *nelp,

size_t width, int ( *compar) (const void *, const void *));

void *lfind (const void *key, const void *base, size_t *nelp,

size_t width, int ( *compar)(const void *, const void *));

Description lsearch() is a linear search routine. It returns a pointer into a table indicating the position

at which a specific value may be found. If the searched value is not present, it is added at
the end of the table. key points to the entry to be sought in the table; base points to the first
element in the table; nelp points to an integer containing the current number of elements in
the table. The integer to which nelp points is incremented if the entry is added to the table.
width is the size of an element in bytes. compar points to a comparison function which the
user must supply (strcmp(), for example). It is called with two arguments that point to the
elements being compared. The function must return 0 if the elements are equal and
non-zero otherwise.

lfind() has the same effect as lsearch() except that if the entry is not found, it is not
added to the table. Instead, a null pointer is returned.

Return val.

*key

lfind(): if successful.
lsearch(): if successful, and also for a newly added element.

Null pointer

lfind(): if an error occurs.

Notes

The comparison function need not compare every byte, so arbitrary data may be contained
in the elements in addition to the values being compared.

Undefined results can occur if there is not enough room in the table to add a new item.

Extension
The pointers to the key and the element at the base of the table may be pointers of any type.

The returned value should be convertible to the type pointer to element. (End)

See also

bsearch(), hsearch(), tsearch(), search.h.