I'm trying to implement a hashtable, in which the table would be an array of "buckets" where each contained user information, my code:
#define tam_inicial 23
typedef struct user{
char nick[6];
char nome[26];
}user;
typedef struct hashtable{
int tam;
user **baldes;
}hashtable;
int tam = tam_inicial;
hashtable * create() {
hashtable *htable = malloc(sizeof(htable));
htable->tam = tam_inicial;
htable->baldes = calloc(tam_inicial, sizeof(htable->baldes));
return htable;
}
int hash(char *string) {
int hashVal = 0;
for( int i = 0; i < strlen(string);i++){
hashVal += (int)string[i];
}
return hashVal % tam;
}
bool isPrime(int num){
if(num==2){
return true;
}
if(num % 2==0){
return false;
}
for(int i=3; i*i<num; i+=2){
if(num%i==0){
return false;
}
}
return true;
}
int Prime_num(int old_size){
for(int i = old_size; i < old_size * 2 +10; i++){
if(isPrime(i) && i >= 2*old_size){
return i;
}
}
}
hashtable *resize_HashTable(hashtable *HashTable){
if(load_factor()){
int tmp = Prime_num(tam);
HashTable = realloc(HashTable, tmp * sizeof(user));
tam = tmp;
}
return HashTable;
}
void inserir(hashtable *HashTable, char *nome, char *nick){
HashTable = resize_HashTable(HashTable);
int hash_value = hash(nick);
while(HashTable->baldes[hash_value] != 0 && hash_value < HashTable->tam){
hash_value++;
}
strcpy(HashTable->baldes[hash_value]->nome, name);
strcpy(HashTable->baldes[hash_value]->nick, nick);
HashTable->tam = HashTable->tam++;
}
For some reason these 2 lines give segmentation fault:
strcpy(HashTable->baldes[hash_value]->nome, name);
strcpy(HashTable->baldes[hash_value]->nick, nick);
It is as if I could not access the struct user's "nickname" and "nickname" variants for there names / nicks
Any reason for this to happen? Thanks in advance.