_lsearch

Performs a linear search for a value; adds to end of list if not found. A more secure version of this function is available; see _lsearch_s.

Syntax

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

Parameters

key
Object to search for.

base
Pointer to the base of array to be searched.

number
Number of elements.

width
Width of each array element.

compare
Pointer to the comparison routine. The first parameter is a pointer to the key for search. The second parameter is a pointer to an array element to be compared with the key.

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 isn't found, _lsearch returns a pointer to the newly added item at the end of the array.

Remarks

The _lsearch function performs a linear search for the value key in an array of number elements, each of width bytes. Unlike bsearch, _lsearch doesn't require the array to be sorted. If key isn't found, _lsearch adds it to the end of the array and increments number.

The compare argument is a pointer to a user-supplied routine that compares two array elements and returns a value specifying their relationship. _lsearch calls the compare routine one or more times during the search, passing pointers to two array elements on each call. compare must compare the elements and return either nonzero (meaning the elements are different) or 0 (meaning the elements are identical).

This function validates its parameters. If compare, key or number is NULL, or if base is NULL and number is nonzero, or if width is less than zero, the invalid parameter handler is invoked, as described in Parameter validation. If execution is allowed to continue, errno is set to EINVAL and the function returns NULL.

By default, this function's global state is scoped to the application. To change this behavior, see Global state in the CRT.

Requirements

Routine Required header
_lsearch <search.h>

For more compatibility information, see Compatibility.

Example

// crt_lsearch.c
#include <search.h>
#include <string.h>
#include <stdio.h>

int compare( const void *arg1, const void *arg2 );

int main(void)
{
   char * wordlist[4] = { "hello", "thanks", "bye" };
                            // leave room to grow...
   int n = 3;
   char **result;
   char *key = "extra";
   int i;

   printf( "wordlist before _lsearch:" );
   for( i=0; i<n; ++i ) printf( " %s", wordlist[i] );
   printf( "\n" );

   result = (char **)_lsearch( &key, wordlist,
                      &n, sizeof(char *), compare );

   printf( "wordlist after _lsearch:" );
   for( i=0; i<n; ++i ) printf( " %s", wordlist[i] );
   printf( "\n" );
}

int compare(const void *arg1, const void *arg2 )
{
   return( _stricmp( * (char**)arg1, * (char**)arg2 ) );
}
wordlist before _lsearch: hello thanks bye
wordlist after _lsearch: hello thanks bye extra

See also

Searching and sorting
bsearch
_lfind
_lsearch_s