C语言 C中文件范围内可变修改的数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13645936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
variably modified array at file scope in C
提问by zch
I have some code like this:
我有一些这样的代码:
static int a = 6;
static int b = 3;
static int Hello[a][b] =
{
{ 1,2,3},
{ 1,2,3},
{ 1,2,3},
{ 1,2,3},
{ 1,2,3},
{ 1,2,3}
};
but when I compile it, it says error:
但是当我编译它时,它说错误:
variably modified 'Hello' at file scope
在文件范围内可变修改“Hello”
how could this happen? and how could I fix it?
这怎么会发生?我怎么能解决它?
回答by zch
You can not have static array which size is given as a variable
你不能有静态数组,其大小作为变量给出
That's why constants should be #defined:
这就是为什么常量应该是#defined:
#define a 6
This way preprocessor will replace awith 6, making it valid declaration.
这样预处理器将替换a为6,使其成为有效的声明。
回答by Omkant
Simple answer variable modified array at file scope is not possible.
简单的回答variable modified array at file scope is not possible。
Detailed :
详细的 :
make it compile time integral constant expression, since array length must be specified at the compile time.
使其成为编译时integral constant expression,因为必须在编译时指定数组长度。
like this :
像这样 :
#define a 6
#define b 3
Or, follow c99 standard. and compile like for gcc.
或者,遵循 c99 标准。并像 gcc 一样编译。
gcc -Wall -std=c99 test.c -o test.out
gcc -Wall -std=c99 test.c -o test.out
The problem here is variable length array with providing length may not be initialized so you are getting this error.
这里的问题是提供长度的可变长度数组可能未初始化,因此您会收到此错误。
simply
简单地
static int a =6;
static int b =3;
void any_func()
{
int Hello [a][b]; // no need of initialization no static array means no file scope.
}
Now use for loop or any loop to fill the array.
现在使用 for 循环或任何循环来填充数组。
For more info just a DEMO :
欲了解更多信息只是一个演示:
#include <stdio.h>
static int a = 6;
int main()
{
int Hello[a]={1,2,3,4,5,6}; // see here initialization of array Hello it's in function
//scope but still error
return 0;
}
root@Omkant:~/c# clang -std=c99 vararr.c -o vararr
vararr.c:8:11: error: variable-sized object may not be initialized
int Hello[a]={1,2,3,4,5,6};
^
1 error generated.
If you remove static and provide initialization then it will generate error as above.
如果您删除静态并提供初始化,那么它会产生如上所述的错误。
But if you keep static as well as initialization the still will be error.
但是,如果您保持静态以及初始化,仍然会出错。
But if you remove initialization and keep staticthe below error will come.
但是,如果您删除初始化并保留static以下错误,则会出现。
error: variable length array declaration not allowed at file scope
static int Hello[a];
^ ~
1 error generated.
So variable length array declaration not allowed at file scope so make it function or block scope inside any function (but remember making it function scope must remove initialization)
因此在文件范围内不允许使用可变长度数组声明,因此在任何函数内使其成为函数或块范围(但请记住使其成为函数范围必须删除初始化)
NOTE : Since it's Ctagged so making aand bas constwon't help you but in C++constwill work fine.
注意:因为它C被标记为 so makinga和basconst对你没有帮助,但 inC++const会正常工作。
回答by PolarBear2015
When using CLANG/LLVM the following works:
使用 CLANG/LLVM 时,以下工作:
static const int a = 6;
static const int b = 3;
static int Hello[a][b] =
{
{ 1,2,3},
{ 1,2,3},
{ 1,2,3},
{ 1,2,3},
{ 1,2,3},
{ 1,2,3}
};
(To see it in the generated assembly, one needs to use Hello so it will not be optimized out)
(要在生成的程序集中看到它,需要使用Hello,所以它不会被优化掉)
However, this will generate an error if C99 mode is selected (-std=c99), it will only generate a warning (Wgnu-folding-constant) if -pedantic is selected.
但是,如果选择 C99 模式(-std=c99),这将产生错误,如果选择 -pedantic,它只会产生警告(Wgnu-folding-constant)。
GCC does not seem to allow this (const is interpreted as read-only)
GCC 似乎不允许这样做(const 被解释为只读)
See explanation in this thread:
请参阅此线程中的说明:
"Initializer element is not constant" error for no reason in Linux GCC, compiling C

