我試圖從字典中的所有單詞實現fnv1a哈希函數(所以我可以稍後訪問它們)。如何在結構中的字符串上實現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;
}
這是如何我試圖讓哈希函數中稱爲負載()的一句話:
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
和問題是什麼? – 2013-03-19 12:07:58
@TonyTheLion它不會編譯。對不起,我忘了注意,但你現在可以在我的問題底部看到錯誤:) – taevanbat 2013-03-19 12:09:19
'ptr-> word'中的'word'類型是什麼? – 2013-03-19 12:10:07