声明具有非恒定大小可变的阵列 [英] Declaring an array with a non-constant size variable

查看:87
本文介绍了声明具有非恒定大小可变的阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学为我用C的测试,我在C总结我从一些网站上下载阅读。
这是写,这是不允许写改编[I] ,其中 I 是一个变量。做到这一点的唯一方法是使用malloc。结果
不过,我写了下面code和它编译没有警告,不会对Valgrind的错误:

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:

    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!

推荐答案

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

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

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

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

这篇关于声明具有非恒定大小可变的阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆