gpt4 book ai didi

c - 如何禁用此特定警告

转载 作者:太空狗 更新时间:2023-10-29 15:05:24 26 4
gpt4 key购买 nike

这个简单的代码:

#define WIDTH 500.5
#define NB 23.2

int x[(int)(WIDTH/NB)];

给我一​​个警告:

prog.c:4:1: warning: variably modified 'x' at file scope [enabled by default]

如果我设置#define WIDTH 500#define NB 23,警告就会消失。

WIDTH 宏传递浮点值会强制编译器进行评估,因此会发出警告,因为数组的大小不是恒定的。

预处理后的 C 代码看起来像 int x[(int)(500.5/23.2)];,而 int x[(int)(500/23)]; 对于编译器来说是可以的(值已经是常量整数)

我也想找个办法

有趣的事情:使用 g++ 编译 我没有收到警告,而我在这里读到可变长度数组在 C++ 中不受官方支持,仅在 C99 中受支持。但这对我来说不是一个选择,因为我需要坚持使用 C。

最佳答案

只是违反了标准:

Integer constant expression

An integer constant expression is anexpression that consists only of operators other than assignment,increment, decrement, function-call, or comma, except that castoperators can only cast arithmetic types to integer types, integerconstants, enumeration constants, character constants, floatingconstants, but only if they are immediately used as operands of caststo integer type

还有:

The following contexts require expressions that are known as integerconstant expressions':

...

  • The index in an array designator (since C99)

关于c - 如何禁用此特定警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39511517/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com