红联Linux门户
Linux帮助

初学者遇到segmentation fault core dumped lfind

发布时间:2013-04-04 18:14:27来源:红联作者:dong101l2123
lz作为linux初学者,今天在学习时遇到了segmentation fault core dumped lfind
具体程序代码如下:

#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);
}
}
}

求各位指正有哪里不对,谢谢
文章评论

共有 0 条评论