hi, is anyone using this library??
I have a problem :
This chunk of code does not work :
Code:
#include <string.h> /* strcpy */
#include <stdlib.h> /* malloc */
#include <stdio.h> /* printf */
#include "uthash.h"
typedef struct hashTableChrInt {
char key[500]; /* key */
int value;
UT_hash_handle hh; /* makes this structure hashable */
}hashTableChrInt;
void add_read( hashTableChrInt * reads, char *read) {
hashTableChrInt *s = NULL;;
int x =10;
s = ( hashTableChrInt*)malloc(sizeof( hashTableChrInt));
strcpy(s->key, read);
s->value = x;
HASH_ADD_STR( reads, key, s );
}
int main() {
char read[] = "Robert";
char name[]="betty";
hashTableChrInt *s, *tmp, *reads = NULL;
add_read(reads,read);
add_read(reads,name);
HASH_FIND_STR( reads, "betty", s);
if (s) printf("betty's id is %d\n", s->value);
/* free the hash table contents */
HASH_ITER(hh, reads, s, tmp) {
HASH_DEL(reads, s);
free(s);
}
return 0;
}
But this one does :
Code:
#include <string.h> /* strcpy */
#include <stdlib.h> /* malloc */
#include <stdio.h> /* printf */
#include "uthash.h"
typedef struct hashTableChrInt{
char key[500]; /* key (string is WITHIN the structure) */
int value;
UT_hash_handle hh; /* makes this structure hashable */
}hashTableChrInt;
int main() {
char read[] = "Robert";
char name[]="betty";
hashTableChrInt *s, *tmp, *reads = NULL;
int i=2;
s = ( hashTableChrInt*)malloc(sizeof(hashTableChrInt));
strcpy(s->key, read);
s->value = i++;
HASH_ADD_STR( reads, key, s );
s = ( hashTableChrInt*)malloc(sizeof( hashTableChrInt));
strcpy(s->key, name);
s->value = i++;
HASH_ADD_STR( reads, key, s );
HASH_FIND_STR( reads, "betty", s);
if (s) printf("betty's id is %d\n", s->value);
/* free the hash table contents */
HASH_ITER(hh, reads, s, tmp) {
HASH_DEL(reads, s);
free(s);
}
return 0;
}
where is my making mistake ?? The only thing i did is to outsource my adding into a function.