_lfind

Performs a linear search for the specified key.

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

Routine Required Header Compatibility
_lfind <search.h> Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

If the key is found, _lfind returns a pointer to the element of the array at base that matches key. If the key is not found, _lfind returns NULL.

Parameters

key

Object to search for

base

Pointer to base of search data

num

Number of array elements

width

Width of array elements

compare

Pointer to comparison routine

elem1

Pointer to key for search

elem2

Pointer to array element to be compared with key

Remarks

The _lfind function performs a linear search for the value key in an array of num elements, each of width bytes in size. Unlike bsearch, _lfind does not require the array to be sorted. The base argument is a pointer to the base of the array to be searched. The compare argument is a pointer to a user-supplied routine that compares two array elements and then returns a value specifying their relationship. _lfind calls the compare routine one or more times during the search, passing pointers to two array elements on each call. The compare routine must compare the elements then return nonzero, meaning the elements are different, or 0, meaning the elements are identical.

Example

/* LFIND.C: This program uses _lfind to search for
 * the word "hello" in the command-line arguments.
 */

#include <search.h>
#include <string.h>
#include <stdio.h>

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

void main( unsigned int argc, char **argv )
{
   char **result;
   char *key = "hello";

   result = (char **)_lfind( &key, argv, 
                      &argc, sizeof(char *), compare );
   if( result )
      printf( "%s found\n", *result );
   else
      printf( "hello not found!\n" );
}

int compare(const void *arg1, const void *arg2 )
{
   return( _stricmp( * (char**)arg1, * (char**)arg2 ) );
}

Output

[C:\code]lfind Hello
Hello found

Searching and Sorting Routines

See Also   bsearch, _lsearch, qsort