声明数组与变量 [英] Declare an array with a variable

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

问题描述

我相信数组的大小应该是恒定的INT,当你把它声明。
不过,我整理了以下code,并没有得到任何错误。
能否谈谈这个解释?

I believe that size of the array should be a constant int when you declare it. However, I compiled the following code and it did not get any error. Could you explain about this?

#include <stdio.h>

void function(int);

int main(void){
  int m = 0;
  scanf("%d", &m);
  function(m);
  return 0;
}

void function(int i){
  int array[i];
}

输入:5
输出:无。但没有得到错误。

input: 5 output: nothing. but got no error.

推荐答案

从C99增加了一个可变长度的数组(或VLA)是自动存储时间的数组,其长度在运行时(而不是在编译确定时间)。

Added from C99 a variable-length array (or VLA) is an array data structure of automatic storage duration whose length is determined at run time (instead of at compile time).

VLA工作通过将阵列中的堆栈。这使得分配和访问速度极快,但堆栈通常是小(几KB的),而当VLA溢出栈,它是从无限递归没有什么区别。

VLA works by placing the array in the stack. This makes allocation and access extremely fast, but the stack is usually small (of a few KB), and when the VLA overflows the stack, it's indistinguishable from an infinite recursion.

这篇关于声明数组与变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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