uthash.h 使用问题

hunterdrf 2010-02-24 09:34:41
从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);
}
...全文
976 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
goinlab 2012-01-16
  • 打赏
  • 举报
回复
add(xy,index,x,y);
执行后,xy还是NULL,因为传入的是值的拷贝,如果想要改变xy的值,就需要
add(&xy,index,x,y);
goinlab 2012-01-16
  • 打赏
  • 举报
回复
问题出在add(xy,index,x,y);
KimenWu 2010-02-26
  • 打赏
  • 举报
回复
快沉下去了,顶一下吧。

顺便说一下,把uthash.h也发上来,我没法调试
KimenWu 2010-02-25
  • 打赏
  • 举报
回复
段错误就是空指针,网上有一些用gdb定位段错误的方法。可以看一下

23,223

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧