【发布时间】:2013-12-08 08:37:12
【问题描述】:
这两种数组定义有什么区别,哪一种更正确,为什么?
#include <stdio.h>
#define SIZE 20
int main() {
// definition method 1:
int a[SIZE];
// end definition method 1.
// defintion method 2:
int n;
scanf("%d", &n);
int b[n];
// end definition method 2.
return 0;
}
我知道如果我们从stdin 读取大小变量n,将我们的(我们将使用的内存块)数组定义为指针并使用stdlib.h 和array = malloc(n * sizeof(int)) 会更正确,而不是将其标记为int array[n],但又是为什么呢?
【问题讨论】:
-
以下答案中未提及的不同之处当然是,方法 1 仅在您知道在设计时(即在编写程序时)最终用户有多少项目时才有效将需要。曾经。使用动态数组,最终用户可以拥有任意数量的项目。
标签: c++ c arrays c-preprocessor