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));
-
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
分享到:
相关推荐
太监最需要UNIX外壳程序命令... :Lfind / :Llocate :和上面一样,但是使用位置列表。 :Wall :编写每个打开的窗口。 方便启动工具。 :SudoWrite :使用sudo编写特权文件。 :SudoEdit :使用sudo编辑特权文件。 sudo -
lsearch(线性搜索) 79 8.5 80 qsort(利用快速排序法排列数组) 80 8.6 81 rand(产生随机数) 81 8.7 81 srand(设置随机数种子) 81 9 文件操作篇 82 9.1 82 close(关闭文件) 82 9.2 82 creat(建立文件) 82 ...
C标准库源代码\LSEARCH.C C标准库源代码\LSEEK.C C标准库源代码\LSEEKI64.C C标准库源代码\MAKEFILE C标准库源代码\MAKEFILE.INC C标准库源代码\MAKEFILE.SUB C标准库源代码\MAKEPATH.C C标准库源代码\MALLOC.C C...
Python_ChatGLM3 series Open Bilingual Chat LLMs 开源双语对话语言模型
附件1:1名男大学生的一日食谱.xlsx
用“假定成功”思路做爆品
STC89C51单片机读取DS18B20的64位序列码并显示在1602液晶上KEIL工程文件+开发板硬件原理图 /*读取DS18B20的64位序列码并显示在1602液晶上,如果读取正确结果,则在 液晶第一行显示DS18B20 OK,第二行显示序列码,如果读取失败,则在液晶上 显示DS18B20 ERR0R PLEASE CHECK ,用户可通过更改18B20接口自己外接。 */ #include <reg51.h> #include <intrins.h> #define uchar unsigned char #define uint unsigned int sbit DQ = P2^2; //定义DS18B20端口DQ sbit BEEP=P2^3 ; //蜂鸣器驱动线 bit presence ; sbit LCD_RS = P3^5; sbit LCD_RW = P3^6; sbit LCD_EN = P3^4; uchar code cdis1[ ] = {" DS18B20 OK "}; uchar code c
实验五 进程间通信.docx
Python_使用oobabooga web的自托管github副驾驶指南
【图像识别】基于计算机视觉实现自动报靶系统(重弹孔)含Matlab源码.zip【图像识别】基于计算机视觉实现自动报靶系统(重弹孔)含Matlab源码_
JLink-Windows-V700
对象检测-使用-yolov5 这是一个基于yolov5算法检测图像中多个物体的模型
企业低代码:业务加速和应用程序现代化的杠杆-2024.pdf
随着科学技术的进步,计算机行业的迅速发展,大大提高人们的工作效率。计算机信息处理系统的引进已彻底改变了许多系统的经营管理。恒鑫租赁站长期大量出租各型钢管、扣件、塔吊等建筑施工设备。 本系统为恒鑫租赁站提供了一个方便快捷的物资管理系统,方便出租物资的管理。系统功能主要包括:用户管理,包括管理员信息查看、管理员密码修改;正常订单管理:包括新增正常订单、修改有误订单、删除错误订单;资金结算管理:费用统计;材料租赁管理:包括订单出租查询、器材回收管理、日租金管理、仓库库存管理。 本系统开发工具采用Myeclipse,数据库设计采用MySQL数据库,开发模式采用JSP为视图层的MVC设计模式。系统界面友好、美观,系统程序冗余度度低,系统具有较强的可扩展性和可维护性。
Python_RunWhen Local为Kubernetes环境提供了定制的故障排除备忘单
Python_尝试PyScript
Python_GitHub安全实验室计划提供一个内部学习体验,让学习者保护有意易受攻击的代码
STC15F2K60S2单片机+FT232RL+TLC5615(DA)+TLC0832(AD)+LCD12864屏开发板AD设计硬件(原理图+PCB)文件,硬件采用2层板设计,大小为142*100mm,Altium软件设计,包括完整地原理图及PCB文件,可供学习及设计参考。
Python_社区支持的增压版无纸化扫描索引和存档您的所有物理文档
Python_基于动作扩散的扩散策略视觉运动策略学习