为什么我们可以在c中这样做?
int n;
scanf("%d",&n);
int a[n];
Run Code Online (Sandbox Code Playgroud)
我认为数组在加载时间内位于内存中,但似乎上述示例在运行时期间有效.我是否误解了任何事情?你能帮忙吗?
谢谢,
I thought array is *al*located memory during load time but seems like the above example works during run-time.
是的,普通数组就像<datatype> <Array_Name> [<size>]在C89中存在的加载时间内分配内存一样,也存在于C99中.
但是在代码片段中int a[n];是一个可变长度数组或简称VLA .C99中的VLA定义与任何其他数组一样,只是长度不需要是编译时常量.
有关需要VLA的文章可以在这里找到:http://www.ddj.com/cpp/184401444 :)
| 归档时间: |
|
| 查看次数: |
316 次 |
| 最近记录: |