c 警告:在常量表达式中使用 const 变量在 C 中是非标准的 [英] c warning: use of const variable in a constant expression is non-standard in C

查看:23
本文介绍了c 警告:在常量表达式中使用 const 变量在 C 中是非标准的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm getting this warning when trying to initialize an array to a constant size.

#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 在我的环境中不可用.代码都有效.我只是想知道为什么我会收到这个错误.

Essentially it's a bit of code that is going to loop over LED error sequences that are added during run time. I've got to use a fixed size array because realloc isn't available in my environment. The code all works. I'm just wondering why I'm getting this error.

推荐答案

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

A const object is not a constant in C but a read-only object. An array declared at file scope (or any array with static storage duration) has to have a constant expression as its number of elements.

这是有效的:

#define MAXERRORS 4
LEDSeq errors[MAXERRORS];

这篇关于c 警告:在常量表达式中使用 const 变量在 C 中是非标准的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆