Hi!
I don't know why I got seg fault when I'm implementing the *creattable() function.
Code:
#define INIT_BUCKETS 5
typedef struct node {
char *key;
char *value;
struct node *next;
} Data_pair_node;
typedef Data_pair_node * Hash_bucket;
struct hash_table {
int key_ct;
int bucket_ct;
Hash_bucket *buckets;
};
typedef struct hash_table Table;
Table *create_table(){
Table *t;
int index;
t = malloc(sizeof(Table));
if(t == NULL){
return NULL;
}
(*t).buckets = malloc(sizeof(Data_pair_node) * INIT_BUCKETS);
for(index = 0; index < INIT_BUCKETS; index++){
*(*t).buckets++ = malloc(sizeof(Data_pair_node));
}
/*gdb tells me the seg fault starts right here, but I dont know what's wrong?*/
for(index = 0; index < INIT_BUCKETS; index++){
(*(*t).buckets[index]).key = malloc(sizeof(char));
(*(*t).buckets[index]).value = malloc(sizeof(char));
} for(index = 0; index < INIT_BUCKETS; index++){
(*(*t).buckets[index]).key = '\0';
(*(*t).buckets[index]).value = '\0';
}
(*t).key_ct = 0;
(*t).bucket_ct = INIT_BUCKETS;
return t;
}