c 警告 : use of const variable in a constant expression is non-standard in C

标签 c warnings

我在尝试将数组初始化为常量大小时收到此警告。

#2170-D 在常量表达式中使用 const 变量在 C 中是非标准的

#file.h
typedef struct {
    // LED Blink Pattern
    .....
} LEDSeq

void addError(LEDSeq);
void runLEDErrors();
....

#file.c
const uint8_t MAXERRORS = 4;
LEDSeq errors[MAXERRORS];
uint8_t errorsLength = 0;
....

本质上,它是一段代码,将循环运行期间添加的 LED 错误序列。我必须使用固定大小的数组,因为 realloc 在我的环境中不可用。代码一切正常。我只是想知道为什么会出现此错误。

最佳答案

const 对象不是 C 中的常量,而是只读对象。在文件范围内声明的数组(或任何具有静态存储持续时间的数组)必须有一个常量表达式作为其元素数。

这是有效的:

#define MAXERRORS 4
LEDSeq errors[MAXERRORS];

关于c 警告 : use of const variable in a constant expression is non-standard in C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24873121/

相关文章:

c - 我如何可视化后递增指针的概念?

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

c - 为什么我的函数返回 0 而不是文件中的行数?

c - mmap 可执行文件可以多次使用吗?

c - gcc 警告 : assignment makes pointer from integer without cast 的未知原因

c - 当变量被隐藏时得到警告

在 OSX 上编译的 ctime 和时间警告

c++ - C 中的 Doxygen 错误

c - vfork() 隐式声明

c - C中扫描语句后执行打印语句