难以用C理解变长数组 [英] Difficulty in understanding variable-length arrays in C
问题描述
我在读一本书时,我发现数组的大小必须在申报时给予或使用malloc在runtime.I堆中分配在C写这个程序:
I was reading a book when I found that array size must be given at time of declaration or allocated from heap using malloc at runtime.I wrote this program in C :
#include<stdio.h>
int main() {
int n, i;
scanf("%d", &n);
int a[n];
for (i=0; i<n; i++) {
scanf("%d", &a[i]);
}
for (i=0; i<n; i++) {
printf("%d ", a[i]);
}
return 0;
}
这code正常工作。
我的问题是如何code可以工作correctly.Isn't它侵犯的C基本概念的数组大小必须在运行之前进行声明或使用malloc()分配它在runtime.I'm没有做任何这两个东西,那么为什么它的工作是否正常?
My question is how this code can work correctly.Isn't it the violation of basic concept of C that array size must be declared before runtime or allocate it using malloc() at runtime.I'm not doing any of these two things,then why it it working properly ?
解决我的问题是这是在C99支持变长数组,但如果我打aroundmy code,并把语句int类型的[N];上述scanf函数(%D,和放大器; N);那么它停止工作为什么so.if变长数组在C的支持
Solution to my question is variable length arrays which are supported in C99 but if I play aroundmy code and put the statement int a[n]; above scanf("%d,&n); then it's stops working Why is it so.if variable length arrays are supported in C ?
推荐答案
C99标准支持变长数组 。这些阵列的长度是在运行时确定的。
The C99 standard supports variable length arrays. The length of these arrays is determined at runtime.
这篇关于难以用C理解变长数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!