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

标签 c

由于有关 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/

相关文章:

c - 以 "int x[][n]"作为参数的函数

c - pthread_join 阻止我的代码

c++ - vs2010 C4353 为什么这不是错误

c - 原始套接字监听器

c - 当类型定义为类型时,Doxygen 不记录变量

强制转换为 C99 中的指针数组

c - fwrite 消耗所有 MemFree,fflush 不工作?

c - 整数变量 :3;

c - 如何获得指向页面开头的指针

c - 什么时候在 C 中使用指向指针的指针来打字双关是合法的?