0

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;
    }
}
2
  • 1
    What compiler error are you getting? Commented Apr 5, 2018 at 18:52
  • Error messages come with line numbers. The line numbers help you know exactly which line the error is on. You should share that information with us. Commented Apr 5, 2018 at 18:54

1 Answer 1

1

You've declared

struct node* ebv_hash = NULL;

and used

ebv_hash = malloc(HASH_SIZE * sizeof(struct node));

This means that you're going to get an array of struct node.

I think what you want is an array of pointers to struct node. That would look like this:

struct node** ebv_hash = NULL;
ebv_hash = malloc(HASH_SIZE * sizeof(struct node*));
for(int i=0;i<HASH_SIZE;i++)
  ebv_hash[i] = NULL;

Then when you need a node i, you malloc yourself one and set the appropriate ebv_hash[i].

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @Richard your answer fixed my problem. A follow up question: As each struct node has a pointer to the next node I thought that an array of struct nodes would work. Why did I require an array of pointers to struct nodes instead?
@daragh: I don't think your struct node should have pointer to next. That's a linked-list/tree/graph idea. The nodes in a hash table should typically not know where they are located with respect to other nodes.
I've added the next pointer within the struct node to allow for collisions when assigning nodes to array locations. Regardless the array of pointers seems to be the way to go. Thanks again.
Ah, then you have an array of pointer to struct node which represent your buckets and then each node is a linked list. So these are separate concepts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.