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);
}
#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);
}
...全文
相关推荐
uthash User Guide
uthash官方说明文档,里面包含对uthash的详细使用说明,目前国外的uthash使用网站无法直接访问,该文档为其官网下载版本。
uthash开源的hash函数实现
uthash开源的hash函数实现,里面的uthash.h可用
uthash源码
很好用的uthash源码。
uthash-开源
这是一个用C实现的哈希表,支持C结构的恒定时间添加/查找/删除。 通过将UT_hash_handle成员添加到该结构并调用这些宏,可以对具有唯一的,任意类型的键成员的任何结构进行哈希处理。
C开源hash代码uthash
uthash 是C的比较优秀的开源代码,它实现了常见的hash操作函数,例如查找、插入、删除等待。该套开源代码采用宏的方式实现hash函数的相关功能,支持C语言的任意数据结构最为key值,甚至可以采用多个值作为key,无论是自定义的struct还是基本数据类型,需要注意的是不同类型的key其操作接口方式略有不通。。 uthash的插入、查找、删除的操作时间都是常量,当然这个常量的值收到key以及所选择的hash函数的影响。


