gpt4 book ai didi

C 中的常量 : Cannot allocate an array of constant size 0

转载 作者:太空狗 更新时间:2023-10-29 14:59:44 27 4
gpt4 key购买 nike

一小段代码:

void func()
{
const int BUF_SIZE = 5;
char scale[BUF_SIZE];
}

此代码在 C++ 下构建良好,但在 C 下我有一个错误:

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0

为什么?

编译器:Microsoft Visual C++ 2008

提前致谢!

最佳答案

在 C 中(我相信所有变体),具有讽刺意味的是,const 不是 C 中的常量表达式。在 C99 之前的版本中,数组长度必须是常量表达式。

但是,C99 具有“可变长度数组”的概念,因此如果您使用 C99 兼容的编译器进行编译,即使 BUF_SIZE 不是常量表达式,您的代码也应该是有效的。

关于C 中的常量 : Cannot allocate an array of constant size 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6290904/

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