【发布时间】:2014-05-13 03:40:36
【问题描述】:
我有一个哈希函数:
unsigned long strhash(char *string)
{
unsigned long hash = 5381;
int c;
while (c = *string++)
{
hash = ((hash << 5) + hash) + c;
}
return hash;
}
我的程序这样称呼它
char testString[] = "Hello World";
unsigned long hashcode = 0;
hashcode = strhash(testString);
int slot = 0;
slot = hashcode%30;
printf("%d\n", slot);
模块是模块我的数组的大小 这是从 unsigned long 安全地转换为 int 吗? 因为它打印出 17 让我觉得它正在工作,但我不确定
【问题讨论】:
-
为什么不让 slot 也无符号呢?负槽/索引对我来说毫无意义。 (顺便说一句:什么是 tmp ?)