声明具有非常量大小变量的数组[重复]

时间:2022-01-24 19:22:15

This question already has an answer here:

这个问题在这里已有答案:

I'm studying for my test in C and I'm reading in a C summary I downloaded from some site. It is written that it is not allowed to write arr[i] where i is a variable. The only way to do it is with malloc.
However, I wrote the following code and it compiles without warnings and without error on valgrind:

我在C学习我的考试,我正在阅读我从某个网站下载的C摘要。写道,不允许写入arr [i],其中i是变量。唯一的方法是使用malloc。但是,我编写了以下代码,它在没有警告的情况下编译,并且在valgrind上没有错误:

int index = 5;
int a4[index];

a4[0] = 1;
a4[1] = 2;

int index2;
scanf("%d",&index2);
int a5[index2];
a5[0] = 1;
a5[1] = 2;

So what is the truth behind array declarations? thank you!

那么数组声明背后的真相是什么呢?谢谢!

1 个解决方案

#1


14  

C99 allows variable length arrays to be created on the stack. Your compiler may support this feature. This features is not available in C89.

C99允许在堆栈上创建可变长度数组。您的编译器可能支持此功能。此功能在C89中不可用。

What the summary told you was true, from a certain point of view. :-)

从某个角度来看,摘要告诉你的是真实的。 :-)

#1


14  

C99 allows variable length arrays to be created on the stack. Your compiler may support this feature. This features is not available in C89.

C99允许在堆栈上创建可变长度数组。您的编译器可能支持此功能。此功能在C89中不可用。

What the summary told you was true, from a certain point of view. :-)

从某个角度来看,摘要告诉你的是真实的。 :-)