_lfind

Description

Performs a linear search for the specified key.

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

void *_lfind( 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 array elements  
width Width of array 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 _lfind function performs a linear search for the value key in an array of num elements; each element is 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. The _lfind 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, _lfind returns a pointer to the element of the array at base that matches key. If the key is not found, _lfind returns NULL.

Compatibility

Standards:UNIX

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

32-Bit:DOS32X

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

See Also

bsearch, _lsearch, qsort

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( void *arg1, void *arg2 );

void main( 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(void *arg1, void *arg2 )

{

return( _stricmp ( * ( char** ) arg1, * ( char** ) arg2 );

}

Output

[C:\LIBREF] _lfind What if I said Hello world

Hello found