lfind - lsearch

lfind - lsearch - Find Key in Array

  •  
    #include <search.h>
    char *lfind(char *search_key, char *base,
                 unsigned int *num, unsigned int *width,
                 int (*compare)(const void *key, const void *element));
    char *lsearch(char *search_key, char *base,
                   unsigned int *num, unsigned int *width,
                   int (*compare)(const void *key, const void *element));
    
  •   Meaning
key and  element are different. key and  element are identical.

lfind and lsearch perform a linear search for the value search_key in an array of num elements, each of width bytes in size. Unlike bsearch, lsearch and lfind do not require that you sort the array first. The argument base points to the base of the array to be searched.

If lsearch does not find the search_key, it adds the search_key to the end of the array and increments num by one. If lfind does not find the search_key, it does not add the search_key to the array.

The compare argument is a pointer to a function you must supply that takes a pointer to the key argument and to an array element, in that order. Both lfind and lsearch call this function one or more times during the search. The function must compare the key and the element and return one of the following values:

Value

Nonzero0

Note: In earlier releases of C Set ++, lfind and lsearch began with an underscore (_lfind and _lsearch). Because they are defined by the X/Open standard, the underscore has been removed. For compatibility, The Developer's Toolkit will map _lfind and _lsearch to lfind and lsearch for you.

 

If search_key is found, both lsearch and lfind return a pointer to that element of the array to which base points. If search_key is not found, lsearch returns a pointer to a newly added item at the end of the array, while lfind returns NULL.

  • This example uses lfind to search for the keyword PATH in the command-line arguments.
#include <search.h>#include <string.h>
#include <stdio.h>

#define  CNT           2

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

int main(void)
{
   char **result;
   char *key = "PATH";
   unsigned int num = CNT;
   char *string[CNT] =  {
      "PATH = d:\\david\\matthew\\heather\\ed\\simon","LIB = PATH\\abc" };

   /* The following statement finds the argument that starts with "PATH"      */

   if ((result = (char **)lfind((char *)&key, (char *)string, &num,
                  sizeof(char *), compare)) != NULL)
      printf("%s found\n", *result);
   else
      printf("PATH not found \n");
   return 0;

   /****************************************************************************
      The output should be:

      PATH = d:\david\matthew\heather\ed\simon found
   ****************************************************************************/
}

Syntax

 

Description

 

Returns

 

Example Code

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值