_lfind
执行线性搜索指定的键。 此功能的一种较为安全的版本可用; _lfind_s参见。
void *_lfind(
const void *key,
const void *base,
unsigned int *num,
unsigned int width,
int (__cdecl *compare)(const void *, const void *)
);
参数
key
搜索的对象。base
要搜索的基础数据的指针。num
数组元素的数目。width
数组元素的宽度。compare
对比较实例的指针。 第一个参数是指向搜索的键。 第二个参数是指向数组元素与密钥进行比较。
返回值
如果找到键时, _lfind 返回指向该数组的元素中的 base 匹配 key。 如果未找到键时, _lfind 返回 NULL。
备注
_lfind 函数在数组 num 组件,每个执行线性搜索该值 keywidth 字节。 不同 bsearch, _lfind 不需要数组进行排序。 base 参数是指向要搜索的数组的基础。 compare 参数是指向比较两个数组元素然后返回指定它们之间的关系的值为用户提供的实例。 要在搜索时,_lfind 调用 compare 实例一次或多次,通过指向在每个的两个数组元素调用。 compare 实例必须比较元素然后返回非零 (表示元素完全不同的) 或 0 (表示组件的相同)。
此功能验证其参数。 如果 compare、 key 或 num 是 NULL,或者,如果 base 为空且 *num 不为零,或者,如果 width 小于零,无效参数调用处理程序,如 参数验证所述。 如果执行允许继续, errno 设置为 EINVAL ,函数返回 NULL。
要求
实例 |
必需的头 |
---|---|
_lfind |
search.h |
有关更多兼容性信息,请参见中介绍的 兼容性 。
示例
// crt_lfind.c
// This program uses _lfind to search a string array
// for an occurrence of "hello".
#include <search.h>
#include <string.h>
#include <stdio.h>
int compare(const void *arg1, const void *arg2 )
{
return( _stricmp( * (char**)arg1, * (char**)arg2 ) );
}
int main( )
{
char *arr[] = {"Hi", "Hello", "Bye"};
int n = sizeof(arr) / sizeof(char*);
char **result;
char *key = "hello";
result = (char **)_lfind( &key, arr,
&n, sizeof(char *), compare );
if( result )
printf( "%s found\n", *result );
else
printf( "hello not found!\n" );
}