1

我正在尝试对字典中的所有单词实现 fnv1a 哈希函数(这样我以后可以快速访问它们)。

这是 fnv1a 哈希函数:

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

这就是我试图在一个名为 load() 的函数中获取一个单词的哈希值的方式:

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

这是完整的功能:

/* * 将字典加载到内存中。如果成功则返回 true,否则返回 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;
}

编译此代码时不断出现的错误(指向我试图散列一个单词的行):

dictionary.c:70:53: error: lvalue required as increment operand
4

2 回答 2

2

似乎 of 的类型是, 或ptr->word的数组。你不能增加一个数组,改变设计。charchar []

于 2013-03-19T12:10:55.013 回答
2

您可以像这样简单地解决您的问题:

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

fnv1a(*ptr++, SEED);
于 2013-03-19T12:12:00.297 回答