uthash.h 使用问题
从pudn上下载了uthash.h 想使用一下哈西表,结果编译运行时在输出查询结果时出现了“段错误”,请教各位高手帮忙解决一下:
#include "uthash.h"
#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
//struct
struct test
{
int x,y;
int index;
UT_hash_handle hh;
};
//add to hash
int add(struct test *hash,int index, int x, int y) {
struct test *s;
s = malloc(sizeof(struct test));
s->x = x;
s->y = y;
HASH_ADD_INT( hash, index, s ); /* id: name of key field */
}
//find in hash
struct test *find(struct test *hash,int index) {
struct test *s;
printf("to 1\n");
HASH_FIND_INT( hash, &index, s ); /* s: output pointer */
printf("to 2\n");
return s;
}
//print rusult
void output(struct test *p)
{
printf("x=%d,",p->x);
printf("y=%d\n",p->y);
}
void main()
{
struct test *xy=NULL;
struct test *result=NULL;
int x,y;
int index=0;
int f;
do
{
printf("Input a pair of x,y:\n(input x=0,y=0 to exit)\n");
printf("x=");
scanf("%d",&x);
printf("y=");
scanf("%d",&y);
add(xy,index,x,y);
index++;
}
while(x!=0&&y!=0);
printf("\n\nInput a index:\n");
scanf("%d",&f);
result=find(xy,f);
output(result);
}