3
// set all values in the hash table to null
for(int i = 0; i < HASH_SIZE; i++)
{
    hashtable[i] = NULL;
}

我不断收到此错误消息以响应 hashtable[i]:

赋值从没有强制转换的指针生成整数 [-Werror]

为什么?

4

2 回答 2

7

Ifhashtable是一个整数数组,则hashtable[i]需要一个整数并且NULL是一个指针。

因此,您尝试将指针值分配给整数变量(没有强制转换),这通常只是一个警告,但因为您-Werror所有的警告都变成了错误。

只需使用0而不是NULL.

于 2012-07-28T02:40:15.367 回答
1

NULL 定义(void*)0stddef.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 将被显示。

于 2012-07-28T11:56:22.413 回答