gpt4 book ai didi

c - 为什么将其强制转换为结构指针而不是编译时常量?

转载 作者:行者123 更新时间:2023-12-01 13:42:33 25 4
gpt4 key购买 nike

由于有关 test_array 的大小不是编译时常量的错误,下面的 C 程序无法使用 gcc 进行编译。为什么?

struct HWND__ { int unused; }; 
typedef struct HWND__ *HWND;

void test()
{
static int test_array[ (unsigned long long)((HWND)1) ];
}

产生的错误是:

test.c: In function ‘test’:
test.c:5:14: error: storage size of ‘test_array’ isn’t constant
static int test_array[ (unsigned long long)((HWND)1) ];
^

最佳答案

来自 C11 标准,第 6.6 节:

An integer constant expression shall have integer type and shall only have operands that are integer constants, enumeration constants, character constants, sizeof expressions whose results are integer constants, _Alignof expressions, 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 or _Alignof operator.

An integer constant expression is required in a number of contexts such as the size of a bit-field member of a structure, the value of an enumeration constant, and the size of a non-variable length array.

我认为这就是您的代码无效的原因。

我建议向 hsc2hs 开发人员提交错误报告,同时手动修复这些情况。

关于c - 为什么将其强制转换为结构指针而不是编译时常量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38763664/

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