为什么不能像这样初始化数组?(复制)

时间:2023-02-14 20:42:21

This question already has an answer here:

这个问题已经有了答案:

#include<stdio.h>

main() {
  int a=5;
  int array[a]={0};
  printf("Success\n");
}

when i am executing the program it will through a error as

当我执行程序时,它会通过一个错误

b.c: In function ‘main’:
b.c:8:1: error: variable-sized object may not be initialized
b.c:8:1: warning: excess elements in array initializer [enabled by default]
b.c:8:1: warning: (near initialization for ‘array’) [enabled by default]

In cc complier . but i can assign like this

在cc依从者。但我可以这样分配。

 int array[5]={0};

If anyone correct me?

如果有人纠正我吗?

3 个解决方案

#1


4  

This statement

这条语句

int array[a]={0};

declares a Variable Length Array (VLA).

声明一个可变长度数组(VLA)。

According to C Standard (6.7.9 Initialization)

根据C标准(6.7.9初始化)

3 The type of the entity to be initialized shall be an array of unknown size or a complete object type that is not a variable length array type.

要初始化的实体的类型应该是一个未知大小的数组,或者一个不属于可变长度数组类型的完整对象类型。

The problem is that the compiler shall know the array size at compile time that to generate the code that initialize an array.

问题是编译器在编译时应该知道要生成初始化数组的代码的数组大小。

Consider an example

考虑一个例子

void f( size_t n )
{
    int a[n] = { 1, 2, 3, 4, 5 };
    //...
}

Here is a is a variable length array. Now as n can have any value then the number of initializers in the array definition can be greater than the size of the array. So this code breaks the Standard from another side because the number of initializers may not be greater than the number of elements of array. On the other hand if the number of initializers less than the number of elements of array then what to do in this case? Maybe the programmer did not mean that some elements shall be zero-initialized.

这是一个可变长度的数组。既然n可以有任何值,那么数组定义中的初始化器的数量可以大于数组的大小。这段代码打破了另一方面的标准,因为初始化器的数量可能不会大于数组元素的数量。另一方面,如果初始化器的数量小于数组元素的个数那么在这种情况下该怎么做呢?也许程序员并不是说某些元素应该是零初始化的。

As for this declaration

至于这个声明

int array[5]={0};

then there is no variable length array. The size of the array is known at compile time. So there is no problem

那么就没有可变长度数组了。数组的大小在编译时是已知的。所以没有问题

.

#2


1  

"Variable-length automatic arrays are allowed in ISO C99, and as an extension GCC accepts them in C90 mode and in C++"

“可变长度的自动数组在ISO C99中是允许的,作为一个扩展,GCC在C90模式和c++中接受它们。”

says here.

在这里说。

Compilers can differ in some situations. It's normal to have problems with variable-sized arrays. This should work if you really need to do this

编译器在某些情况下可能会有所不同。变量大小的数组出现问题是很正常的。如果你真的需要这样做的话,这应该是可行的

#include<stdio.h>
#DEFINE A 5

 main()
 {
 int array[A]={0};
 printf("Success\n");
 }

#3


0  

The elements field within square brackets [], representing the number of elements in the array, must be a constant expression, since arrays are blocks of static memory whose size must be determined at compile time, before the program runs.

方括号[]中的元素字段表示数组中的元素数量,必须是一个常量表达式,因为数组是静态内存块,其大小必须在程序运行之前的编译时确定。

#1


4  

This statement

这条语句

int array[a]={0};

declares a Variable Length Array (VLA).

声明一个可变长度数组(VLA)。

According to C Standard (6.7.9 Initialization)

根据C标准(6.7.9初始化)

3 The type of the entity to be initialized shall be an array of unknown size or a complete object type that is not a variable length array type.

要初始化的实体的类型应该是一个未知大小的数组,或者一个不属于可变长度数组类型的完整对象类型。

The problem is that the compiler shall know the array size at compile time that to generate the code that initialize an array.

问题是编译器在编译时应该知道要生成初始化数组的代码的数组大小。

Consider an example

考虑一个例子

void f( size_t n )
{
    int a[n] = { 1, 2, 3, 4, 5 };
    //...
}

Here is a is a variable length array. Now as n can have any value then the number of initializers in the array definition can be greater than the size of the array. So this code breaks the Standard from another side because the number of initializers may not be greater than the number of elements of array. On the other hand if the number of initializers less than the number of elements of array then what to do in this case? Maybe the programmer did not mean that some elements shall be zero-initialized.

这是一个可变长度的数组。既然n可以有任何值,那么数组定义中的初始化器的数量可以大于数组的大小。这段代码打破了另一方面的标准,因为初始化器的数量可能不会大于数组元素的数量。另一方面,如果初始化器的数量小于数组元素的个数那么在这种情况下该怎么做呢?也许程序员并不是说某些元素应该是零初始化的。

As for this declaration

至于这个声明

int array[5]={0};

then there is no variable length array. The size of the array is known at compile time. So there is no problem

那么就没有可变长度数组了。数组的大小在编译时是已知的。所以没有问题

.

#2


1  

"Variable-length automatic arrays are allowed in ISO C99, and as an extension GCC accepts them in C90 mode and in C++"

“可变长度的自动数组在ISO C99中是允许的,作为一个扩展,GCC在C90模式和c++中接受它们。”

says here.

在这里说。

Compilers can differ in some situations. It's normal to have problems with variable-sized arrays. This should work if you really need to do this

编译器在某些情况下可能会有所不同。变量大小的数组出现问题是很正常的。如果你真的需要这样做的话,这应该是可行的

#include<stdio.h>
#DEFINE A 5

 main()
 {
 int array[A]={0};
 printf("Success\n");
 }

#3


0  

The elements field within square brackets [], representing the number of elements in the array, must be a constant expression, since arrays are blocks of static memory whose size must be determined at compile time, before the program runs.

方括号[]中的元素字段表示数组中的元素数量,必须是一个常量表达式,因为数组是静态内存块,其大小必须在程序运行之前的编译时确定。