// set all values in the hash table to null
for(int i = 0; i < HASH_SIZE; i++)
{
hashtable[i] = NULL;
}
我保持在响应收到这个错误讯息到hashtable中[I]:分配时将指针整数,未作施放[-Werror]
赋值时将指针整数,未作铸造[-Werror]
为什么?
// set all values in the hash table to null
for(int i = 0; i < HASH_SIZE; i++)
{
hashtable[i] = NULL;
}
我保持在响应收到这个错误讯息到hashtable中[I]:分配时将指针整数,未作施放[-Werror]
赋值时将指针整数,未作铸造[-Werror]
为什么?
如果hashtable
是一个整数数组,则hashtable[i]
需要一个整数,而NULL
是一个指针。
所以你试图给一个整型变量指定一个指针值(没有强制转换),这通常只是一个警告,但是因为你有-Werror
所有的警告都会变成错误。使用0
代替NULL
。
NULL在stddef.h
#ifndef _LINUX_STDDEF_H
#define _LINUX_STDDEF_H
#undef NULL
#if defined(__cplusplus)
#define NULL 0
#else
#define NULL ((void *)0)
#endif
#endif
如果散列表是整数数组定义为(void*)0
,像
#include <stdio.h>
#define HASH_SIZE 100
int main()
{
int i = 0, hashtable[HASH_SIZE];
for(i = 0; i < HASH_SIZE; i++)
{
hashtable[i] = NULL;
}
return 0;
}
此warning: assignment makes integer from pointer without a cast
将被显示。
啊,我明白了。我也可以声明一个指针数组并存储这个值。我现在明白了。谢谢! – hannah 2012-07-28 02:53:47