为什么不能将可变长度数组作为全局变量工作? [英] Why doesn't work Variable length array as a globally?
本文介绍了为什么不能将可变长度数组作为全局变量工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我将可变长度数组写为本地,则像这样:
If I write variable length array as a locally, like this:
#include <stdio.h>
int main()
{
int i=1;
int a[i];
printf("%zu",sizeof(a));
}
在GCC编译器中工作正常.
It working fine in GCC compiler.
但是,如果我将可变长度数组写为全局变量,就像这样:
But, If I write Variable length array as a globally, like this:
#include <stdio.h>
int i=1;
int a[i];
int main()
{
printf("%zu",sizeof(a));
}
然后,GCC编译器给出以下错误:
Then, GCC compiler gives following an error:
prog.c:4:5: error: variably modified 'a' at file scope
int a[i];
推荐答案
从标准 6.7.6.2
如果标识符被声明为具有静态或线程的对象存储持续时间,不得具有可变长度的数组类型.
If an identifier is declared to be an object with static or thread storage duration, it shall not have a variable length array type.
这篇关于为什么不能将可变长度数组作为全局变量工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文