1
votes

I'm trying to implement the fnv1a hash function on all the words from a dictionary (so I can access them quickly later on).

This is the fnv1a hash function:

int
fnv1a(unsigned char byte, uint32_t hash)
{
    hash = SEED;
    // SEED is a constant that I defined
    return ((byte ^ hash) * PRIME) % HASHTABLE_SIZE;
}

And this is how I'm trying to get the hash for a word in a function called load():

int hash = fnv1a((unsigned char)*(ptr->word)++, SEED);

Here's the full function:

/* * Loads dictionary into memory. Returns true if successful else false. */

bool
load(const char *dictionary)
{    
    FILE* fp = fopen("words.txt", "r");

    // make new node to store the stuff in
    node *ptr = malloc(sizeof(node));
    ptr->next = NULL;

    // while it's not the end of the file
    while(!feof(fp))
    {
        // store the word in the ptr
        fscanf(fp, "%s", ptr->word);

        // get hash function for word
        int hash = fnv1a((unsigned char)*(ptr->word)++, SEED);

        // store word at hash value in hashtable

        // if there isn't a word there yet
        if (hashtable[hash]->next == NULL) 
            hashtable[hash]->next = ptr;  

    // else go to the end of the list and add the word

    // haven't done this part yet

    if (hashtable == NULL)
    {
        printf("Didn't work out, bud");
        return false;
    }   

    else 
        return true;
}

The error that I keep getting when I compile this code (points to the line with me trying to hash a word):

dictionary.c:70:53: error: lvalue required as increment operand
2
@TonyTheLion It won't compile. Sorry I forgot to note that, but you can see the error at the bottom of my question now :) - taevanbat
what's the type of word in ptr->word? - Tony The Lion
@TonyTheLion It seems it's char []. - user529758
char[] don't have operator++ defined on them. - Tony The Lion

2 Answers

2
votes

It seems that the type of ptr->word is an array of char, or char []. You can't increment an array, change design.

2
votes

You could simply solve your problem like so:

char* ptr = &ptr->word[0];

fnv1a(*ptr++, SEED);