是否允许/正确定义零长度可变长度数组? [英] Are zero-length variable length arrays allowed/well defined?

查看:110
本文介绍了是否允许/正确定义零长度可变长度数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在C99中编程,并且在代码的一部分中使用了可变长度数组。我知道在C89中不允许使用零长度数组,但是我不确定C99和可变长度数组。

I'm programming in C99 and use variable length arrays in one portion of my code. I know in C89 zero-length arrays are not allowed, but I'm unsure of C99 and variable length arrays.

简而言之,以下定义明确的行为是吗?

In short, is the following well defined behavior?

int main()
{
    int i = 0;
    char array[i];
    return 0;
}


推荐答案

不,零长度数组被C语言明确禁止,即使它们是通过运行时大小值(如您的代码示例中)作为VLA创建的。

No, zero-length arrays are explicitly prohibited by C language, even if they are created as VLA through a run-time size value (as in your code sample).


6.7.5.2数组声明符

...

5 如果大小是不是整数常量
表达式:如果它出现在函数原型范围的声明中,则
会被视为用*替换; 否则,每次对
进行评估时,其值都应大于零

5 If the size is an expression that is not an integer constant expression: if it occurs in a declaration at function prototype scope, it is treated as if it were replaced by *; otherwise, each time it is evaluated it shall have a value greater than zero.

这篇关于是否允许/正确定义零长度可变长度数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆