为什么我不能创建一个由全局变量决定大小的数组呢?

时间:2021-05-24 20:42:33

Why does the array a not get initialized by global variable size?

为什么数组a没有被全局变量大小初始化?

#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.

在我看来,数组应该按大小初始化。

And what would be the answer if I insist on using global variable (if it is possible)?

如果我坚持使用全局变量(如果可能的话),答案是什么?

7 个解决方案

#1


21  

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.

如果大小是一个整数常量表达式,而元素类型具有已知的常量大小,则数组类型不是可变长度的数组类型;否则,数组类型是可变长度数组类型。

a has variable length array type, because size is not an integer constant expression. Thus, it cannot have an initializer list.

a具有可变长度数组类型,因为大小不是整数常量表达式。因此,它不能有初始化列表。

In C90, there are no VLAs, so the code is illegal for that reason.

在C90中,没有VLAs,因此代码是非法的。

In C++ there are also no VLAs, but you could make size a const int. That's because in C++ you can use const int variables in ICEs. In C you can't.

在c++中也没有VLAs,但是您可以将大小设置为const int.因为在c++中,您可以在ice中使用const int变量。在C语言中你不能。

Presumably you didn't intend a to have variable length, so what you need is:

假设你不想让a有可变长度,所以你需要的是:

#define size 5

If you actually did intend a to 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的情况下,很难说应该发生什么。为可变长度数组指定固定大小的初始值实际上没有意义。

#2


9  

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(一个[0])的大小:

int size = sizeof(a) / sizeof(a[0]);

#3


4  

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()获得控制时,编译器不能假定大小值仍然为5。如果您想要在旧式C项目中得到一个真正的常量,请使用:

#define size 5

#4


3  

size is 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 malloc or make the size a constant.

size是一个变量,而C不允许声明(编辑:C99允许您声明它们,只是不像您正在做的那样初始化它们)具有可变大小的数组。如果要创建一个大小为变量的数组,请使用malloc或将大小设置为常量。

#5


3  

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需要通过开关化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....

编辑:你可以在这里找到更多关于C99标准的信息……这里....

#6


1  

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.

编译器在声明时需要知道数组的大小。因为数组的大小在声明之后不会改变。如果将数组的大小放入一个变量中,您可以想象当程序执行时该变量的值将会改变。在这种情况下,编译器将*为这个数组分配额外的内存。在这种情况下,这是不可能的,因为数组是在堆栈上分配的静态数据结构。我希望这能有所帮助。

#7


0  

#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 size means that size is a variable and C does not allow variablesize arrays.

int size意味着size是一个变量,而C不允许变量数组。

I am using VS2008 where using

我正在使用VS2008

const int size=5;  

allows

允许

int a[size]={1,2,3,4,5}; 

#1


21  

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.

如果大小是一个整数常量表达式,而元素类型具有已知的常量大小,则数组类型不是可变长度的数组类型;否则,数组类型是可变长度数组类型。

a has variable length array type, because size is not an integer constant expression. Thus, it cannot have an initializer list.

a具有可变长度数组类型,因为大小不是整数常量表达式。因此,它不能有初始化列表。

In C90, there are no VLAs, so the code is illegal for that reason.

在C90中,没有VLAs,因此代码是非法的。

In C++ there are also no VLAs, but you could make size a const int. That's because in C++ you can use const int variables in ICEs. In C you can't.

在c++中也没有VLAs,但是您可以将大小设置为const int.因为在c++中,您可以在ice中使用const int变量。在C语言中你不能。

Presumably you didn't intend a to have variable length, so what you need is:

假设你不想让a有可变长度,所以你需要的是:

#define size 5

If you actually did intend a to 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的情况下,很难说应该发生什么。为可变长度数组指定固定大小的初始值实际上没有意义。

#2


9  

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(一个[0])的大小:

int size = sizeof(a) / sizeof(a[0]);

#3


4  

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()获得控制时,编译器不能假定大小值仍然为5。如果您想要在旧式C项目中得到一个真正的常量,请使用:

#define size 5

#4


3  

size is 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 malloc or make the size a constant.

size是一个变量,而C不允许声明(编辑:C99允许您声明它们,只是不像您正在做的那样初始化它们)具有可变大小的数组。如果要创建一个大小为变量的数组,请使用malloc或将大小设置为常量。

#5


3  

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需要通过开关化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....

编辑:你可以在这里找到更多关于C99标准的信息……这里....

#6


1  

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.

编译器在声明时需要知道数组的大小。因为数组的大小在声明之后不会改变。如果将数组的大小放入一个变量中,您可以想象当程序执行时该变量的值将会改变。在这种情况下,编译器将*为这个数组分配额外的内存。在这种情况下,这是不可能的,因为数组是在堆栈上分配的静态数据结构。我希望这能有所帮助。

#7


0  

#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 size means that size is a variable and C does not allow variablesize arrays.

int size意味着size是一个变量,而C不允许变量数组。

I am using VS2008 where using

我正在使用VS2008

const int size=5;  

allows

允许

int a[size]={1,2,3,4,5};