Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer


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 *, const void *)


Object to search for.
Pointer to base of search data.
Number of array elements.
Width of array elements.
Pointer to comparison routine. The first parameter is a pointer to key for search. The second parameter is a pointer to array element to be compared with key.

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.


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.


Routine Required header Compatibility
_lfind <search.h> Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.


All versions of the C run-time libraries.


// crt_lfind.c
// arguments: Hello
/* 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 );

int 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 );
      printf( "hello not found!\n" );

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


Hello found

See Also

Searching and Sorting Routines | bsearch | _lsearch | qsort | Run-Time Routines and .NET Framework Equivalents

© 2015 Microsoft