gpt4 book ai didi

c - 文件范围内的可变修改数组

转载 作者:太空狗 更新时间:2023-10-29 17:06:08 26 4
gpt4 key购买 nike

下面的代码有什么问题?

#define DELAY_CYCLES ((int)(0.1/0.001))typedef struct {    double state_history[N_X][DELAY_CYCLES];    double foo;} foo

gcc 提示:

main.h:52:3: warning: variably modified 'state_history' at file scope

是否因为某些原因无法在编译时完成 int 转换?

最佳答案

再次编辑

如果您严格遵守标准,那么是的,您应该避免在那里使用浮点表达式。在 C 中,除了将浮点常量强制转换为整数(例如 (int)3.0f)外,浮点表达式在编译时不被视为数组常量表达式尺寸计算。您需要修改定义以避免 float 并专门使用整数。

为了不成为变长数组,要求数组大小为“整型常量表达式”(C99 §6.7.5.2/4),在§6.6/6中定义了一个“整型常量表达式”(强调我的):

An integer constant expression96) shall have integer type and shall only have operands that are integer constants, enumeration constants, character constants, sizeof expressions whose results are integer constants, and floating constants that are the immediate operands of casts. Cast operators in an integer constant expression shall only convert arithmetic types to integer types, except as part of an operand to the sizeof operator.

GCC 似乎只在 4.5 版中添加了该警告。在 4.4 及以下版本中,它不会报告该代码的任何警告,即使使用 -Wall -Wextra -ansi -pedantic。但是,为了安全和 100% 可移植,您应该更改 DELAY_CYCLES 的定义以避免浮点表达式。

关于c - 文件范围内的可变修改数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4789686/

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