具体程序代码如下:
#include
#include
#include
#define NMEMB 50
#define SIZE 10
int compar (const void *a,const void *b)
{
return (strcmp((char *) a, (char *) b));
}
main()
{
char data[NMEMB][SIZE]={"Linux","freebsd","solzris","sunos","windows"};
char key[80],*base,*offset;
base = data[0];
int i, nmemb=NMEMB,size=SIZE;
for(i=1;i<5;i++)
{
fgets(key,sizeof(key),stdin);
key[strlen(key)-1]='\0';
offset = (char *)lfind(key,base,&nmemb,size,compar);
if(offset ==NULL)
{
printf("%s not found!\n",key);
offset=(char *) lsearch(key,base,&nmemb,size,compar);
printf("Add %s to data array\n",offset);
}
else
{
printf("found : %s \n",offset);
}
}
}
求各位指正有哪里不对,谢谢