C语言 为什么我不能创建一个大小由全局变量决定的数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2427336/
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
Why can't I create an array with size determined by a global variable?
提问by Ashish Yadav
Why does the array anot get initialized by global variable size?
为什么数组a没有被全局变量初始化size?
#include<stdio.h>
int size = 5;
int main()
{
int a[size] = {1, 2, 3, 4, 5};
printf("%d", a[0]);
return 0;
}
The compilation error is shown as
编译错误显示为
variable-sized object may not be initialized
可变大小的对象可能不会被初始化
According to me, the array should get initialized by size.
根据我的说法,数组应该由size.
And what would be the answer if I insist on using global variable (if it is possible)?
如果我坚持使用全局变量(如果可能的话),答案是什么?
回答by Steve Jessop
In C99, 6.7.8/3:
在 C99 中,6.7.8/3:
The type of the entity to be initialized shall be an array of unknown size or an object type that is not a variable length array type.
要初始化的实体类型应为未知大小的数组或非可变长度数组类型的对象类型。
6.6/2:
6.6/2:
A constant expression can be evaluated during translation rather than runtime
可以在翻译期间而不是运行时评估常量表达式
6.6/6:
6.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, and floating constants that are the immediate operands of casts.
整数常量表达式应具有整数类型,并且只能具有整数常量、枚举常量、字符常量、结果为整数常量的 sizeof 表达式和作为转换的直接操作数的浮点常量的操作数。
6.7.5.2/4:
6.7.5.2/4:
If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type; otherwise, the array type is a variable length array type.
如果大小为整型常量表达式且元素类型具有已知常量大小,则数组类型不是变长数组类型;否则,数组类型为变长数组类型。
ahas variable length array type, because sizeis not an integer constant expression. Thus, it cannot have an initializer list.
a具有变长数组类型,因为size不是整数常量表达式。因此,它不能有初始化列表。
In C90, there are no VLAs, so the code is illegal for that reason.
在 C90 中,没有 VLA,因此代码是非法的。
In C++ there are also no VLAs, but you could make sizea const int. That's because in C++ you can use const intvariables in ICEs. In C you can't.
在 C++ 中也没有 VLA,但您可以创建size一个const int. 那是因为在 C++ 中,您可以const int在 ICE 中使用变量。在 C 中你不能。
Presumably you didn't intend ato have variable length, so what you need is:
大概你不打算a有可变长度,所以你需要的是:
#define size 5
If you actually did intend ato have variable length, I suppose you could do something like this:
如果您确实打算a使用可变长度,我想您可以执行以下操作:
int a[size];
int initlen = size;
if (initlen > 5) initlen = 5;
memcpy(a, (int[]){1,2,3,4,5}, initlen*sizeof(int));
Or maybe:
或者可能:
int a[size];
for (int i = 0; i < size && i < 5; ++i) {
a[i] = i+1;
}
It's difficult to say, though, what "should" happen here in the case where size != 5. It doesn't really make sense to specify a fixed-size initial value for a variable-length array.
但是,很难说在 size != 5 的情况下“应该”发生什么。为可变长度数组指定固定大小的初始值实际上没有意义。
回答by indiv
You don't need to tell the compiler what size the array is if you're giving an initializer. The compiler will figure out the size based on how many elements you're initializing it with.
如果您提供初始化程序,则无需告诉编译器数组的大小。编译器将根据您初始化它的元素数量来计算大小。
int a[] = {1,2,3,4,5};
Then you can even let the compiler tell you the size by getting the total size of the array in bytes sizeof(a)and dividing it by the size of one element sizeof(a[0]):
然后你甚至可以让编译器通过获取数组的总大小(以字节为单位)sizeof(a)并将其除以一个元素的大小来告诉你大小sizeof(a[0]):
int size = sizeof(a) / sizeof(a[0]);
回答by Seva Alekseyev
The compiler cannot assume that the value of size is still 5 by the time main() gets control. If you want a true constant in an old-style C project, use:
编译器不能假设在 main() 获得控制权时 size 的值仍然是 5。如果您想在旧式 C 项目中使用真正的常量,请使用:
#define size 5
回答by t0mm13b
It looks like that your compiler is not C99 Compliant...speaking of which, which compiler are you using? If it's gcc you need to pass the switch '-std=c99'.... if you are using a pre-C99 compiler, that statement is illegal, if that's the case, do this:
看起来您的编译器不符合 C99 标准...说到哪,您使用的是哪个编译器?如果是 gcc,则需要传递开关 '-std=c99'.... 如果您使用的是 C99 之前的编译器,则该语句是非法的,如果是这种情况,请执行以下操作:
int main() {
int a[5]={1,2,3,4,5};
printf("%d",a[0]);
return 0;
}
In pre-C99 standard compilers, use a constant instead of a variable.
在 C99 之前的标准编译器中,使用常量而不是变量。
Edit:You can find out more about the C99 standard here... and here....
回答by IVlad
sizeis a variable, and C does not allow you to declare (edit:C99 allows you to declare them, just not initialize them like you are doing) arrays with variable size like that. If you want to create an array whose size is a variable, use mallocor make the size a constant.
size是一个变量,C 不允许你声明(编辑:C99 允许你声明它们,只是不像你正在做的那样初始化它们)具有可变大小的数组。如果要创建大小为变量的数组,请使用malloc或将大小设为常量。
回答by Abderrahmen
The compiler needs to know the size of the array while declaring it. Because the size of an array doesn't change after its declaration. If you put the size of the array in a variable, you can imagine that the value of that variable will change when the program is executed. In this case, the compiler will be forced to allocate extra memory to this array. In this case, this is not possible because the array is a static data structure allocated on the stack. I hope that this will help.
编译器在声明数组时需要知道数组的大小。因为数组的大小在声明后不会改变。如果把数组的大小放在一个变量中,可以想象在程序执行时那个变量的值会发生变化。在这种情况下,编译器将被迫为此数组分配额外的内存。在这种情况下,这是不可能的,因为数组是分配在堆栈上的静态数据结构。我希望这会有所帮助。
回答by Pratik Deoghare
#include<stdio.h>
/* int size=5; */
#define size 5 /* use this instead*/
/*OR*/
int a[size]={1,2,3,4,5}; /* this*/
int main()
{
int a[size]={1,2,3,4,5};
printf("%d",a[0]);
return 0;
}
int sizemeans that sizeis a variableand C does not allow variablesizearrays.
int size意味着这size是一个变量,C 不允许变量size数组。
I am using VS2008 where using
我正在使用 VS2008 在哪里使用
const int size=5;
allows
允许
int a[size]={1,2,3,4,5};

