Function name : lfind
Header file : <stdlib.h>
Function prototype : void* lfind(void* key,void* district,int *n,int m,
int (*func)(const void*,const void*));
Function : Used to perform a linear search from beginning to end within a given area
Parameters : void* key pointer to the keyword to be found
void* district pointer to the district to be found
int *n result buffer after search
int m width of the area to be found
int (*func)(const void*,const void*) A pointer to a function. This function is used to compare the sizes of two elements.
Return value: If the key data is found, return the address of the found element; otherwise, return NULL;
Program example: Use this function to linearly search for element 68 in array a, and output a prompt
#include<stdio.h>#include<stdlib.h>typedefint(*fc)(constvoid*,constvoid*);intcompare(constvoid*p1,constvoid*p2){//Compare the size of two numbers int*pi1= (int*)p1;int*pi2=(int*)p2;return(*pi1-*pi2);}intmain(void){intarr[5]={25,14,29,68,55};size_tn= 5;intkey=29;fcf=compare;int*result=(int*)lfind(&key,arr,&n,sizeof(int),f);if(result){printf(Number%disfoundn,key); }else{printf(Number%disnotfoundn,key);}return0;}
Running results
Number29isfound