#include"stdio.h"
#include"stdlib.
#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;
int i, nmemb=NMEMB,size=SIZE;
struct timeval tv1,tv2;
struct timezone tz
fgets(key,sizeof(key),stdin);
key[strlen(key)-1]=*\0*;
base = data[0];
offset = (char *)bsearch(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);
}
}
程序很简单,仿照写的二分查找,LINUX下运行总是报错 段错误 (核心已转储)
新手不懂。求教。
#include"stdlib.
#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;
int i, nmemb=NMEMB,size=SIZE;
struct timeval tv1,tv2;
struct timezone tz
fgets(key,sizeof(key),stdin);
key[strlen(key)-1]=*\0*;
base = data[0];
offset = (char *)bsearch(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);
}
}
程序很简单,仿照写的二分查找,LINUX下运行总是报错 段错误 (核心已转储)
新手不懂。求教。