我为散列函数编写了这个块:
#include "debug.h"
#include "hash.h"
unsigned hash_add(unsigned char *name)
{
unsigned h;
for(h=0;*name;h+=name++);
return h;
}
我一直得到:
hashadd.c:7:17: warning: assignment makes integer from pointer without a cast [enabled by default]
答案 0 :(得分:2)
for``h+=name++
的最后一个表达式是将地址(name
的类型为unsigned char *
)分配给h
(类型unsigned int
)。将其更改为
for(h = 0; *name; h += *name++);
^Dereference the pointer before assigning to h.
答案 1 :(得分:1)
为什么不通过保持哈希初始化,迭代和累积分开来以自然的方式编写代码?
unsigned hash_add(const unsigned char *name) {
unsigned h = 0;
for(; *name; name++) h += *name;
return h;
}