// set all values in the hash table to null
for(int i = 0; i < HASH_SIZE; i++)
{
hashtable[i] = NULL;
}
我不断收到此错误消息以响应 hashtable[i]:
赋值从没有强制转换的指针生成整数 [-Werror]
为什么?
Ifhashtable
是一个整数数组,则hashtable[i]
需要一个整数并且NULL
是一个指针。
因此,您尝试将指针值分配给整数变量(没有强制转换),这通常只是一个警告,但因为您-Werror
所有的警告都变成了错误。
只需使用0
而不是NULL
.
NULL 定义(void*)0
为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
如果哈希表是整数数组,比如
#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
将被显示。