_lfind

執行線性搜尋指定的機碼。更安全版本之這個函式是可使用; see _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函式會執行線性搜尋值key的陣列中num項目,每個width個位元組。不像bsearch, _lfind並不需要排序陣列。base引數是陣列,要搜尋的基底指標。compare引數為使用者提供的常式會比較兩個陣列項目,則會傳回值,指定兩者的關聯性的指標。_lfind呼叫compare搜尋,傳遞至兩個陣列元素的指標,在每個呼叫期間日常的一或多次。compare常式必須比較項目,則會傳回非零值 (亦即不相同的項目) 或 0 (也就相同的項目)。

這個函式會驗證它的參數。如果compare, key或num是NULL,或是否base為 NULL 和 *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" );
}
  

.NET Framework 對等用法

System::Collections::ArrayList:: 包含

請參閱

參考

搜尋和排序

_lfind_s

bsearch

_lsearch

qsort