I have a file with ~6 million records (key value pairs) that I am trying to store in a hash table. However, I am falling at the first fence. The program will not compile and I have not even attempted to read in the records yet.
Firstly, when trying to initialise the whole array I am getting the following error:
31: error: incompatible types when assigning to type ‘struct node’ from type ‘void *’
Perhaps I should make everything inside my struct node equal to null? But is it not better that the array locations are equal to null?
Also, in my insert function, when checking if the array location is null I get this error:
invalid type argument of unary ‘*’ (have ‘struct node’)
How do I check if the array location is empty? This error is from lines 63, 65, 69, 71, 76
I have read many posts on here (including Why whole structure can not be compared in C, yet it can be copied?) but cannot get my code to compile. Apologies if this is basic stuff.
Full code I have so far:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HASH_SIZE 1000000
struct node{
unsigned long long key;
float value;
struct node *next;
};
struct node *ebv_hash = NULL;
unsigned long long hash(unsigned long long);
void insert(unsigned long long, float);
int main(int argc, char *argv[]){
FILE *ebv_fp;
ebv_fp=fopen(argv[1], "r");
ebv_hash = malloc(HASH_SIZE * sizeof(struct node));
for(unsigned long long a=0; a <=HASH_SIZE; a++){
*ebv_hash[a]=NULL;
}
/* Code to read in data I have not written yet */
fclose(ebv_fp);
free(ebv_hash);
return 0;
}
unsigned long long hash(unsigned long long ani_id){
return (ani_id % HASH_SIZE);
}
void insert(unsigned long long nkey, float nvalue){
struct node *nani = malloc(sizeof(struct node));
unsigned long long hash_ind;
nani->key=nkey;
nani->value=nvalue;
nani->next=NULL;
hash_ind = hash(nkey);
if(ebv_hash[hash_ind] == NULL){
ebv_hash[hash_ind] = nani;
}else{
struct node *p = malloc(sizeof(struct node));
p = ebv_hash[hash_ind];
while(p->next != NULL){
p = p->next;
}
p->next = nani;
}
}