_lsearch

Description

Performs a linear search for a value; adds to end of list if not found.

#include <search.h> Required only for function declarations  

void *_lsearch( const void *key, const void *base, unsigned int *num,
unsigned int width, int ( __cdecl *compare )( const void *elem1,
const void *elem2 ) );

key Object to search for  
base Pointer to base of search data  
num Number of elements  
width Width of elements  
compare Pointer to comparison routine  
elem1 Pointer to the key for the search  
elem2 Pointer to the array element to be compared with the key  

Remarks

The _lsearch function performs a linear search for the value key in an array of num elements, each of width bytes in size. (Unlike bsearch, _lsearch does not require the array to be sorted.) The base argument is a pointer to the base of the array to be searched.

If key is not found, _lsearch adds it to the end of the array.

The compare argument is a pointer to a user-supplied routine that compares two array elements and returns a value specifying their relationship. The _lsearch function calls the compare routine one or more times during the search, passing pointers to two array elements on each call. This routine must compare the elements, then return one of the following values:

Value Meaning

Nonzero Elements are different
0 Elements are identical

Return Value

If the key is found, _lsearch returns a pointer to the element of the array at base that matches key. If the key is not found, _lsearch returns a pointer to the newly added item at the end of the array.

Compatibility

Standards:UNIX

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

Use _lsearch for compatibility with ANSI naming conventions of non-ANSI functions. Use lsearch and link with OLDNAMES.LIB for UNIX compatibility.

See Also

bsearch, _lfind

Example

See the example for _lfind.