【发布时间】:2016-03-18 07:52:26
【问题描述】:
我想在 C 中声明一个全局二维数组,并在运行时分配 连续 内存,因为在编译时次要维度是未知的。 我想用 2 索引符号 A[i][j] 取消对数组的引用。 如果数组不是全局 c99 表示法“double A[m][n]”会很方便,但在我的情况下不适用。 什么是正确的策略?
#include <stdio.h>
#include <stdlib.h>
//TO DO
//DECLARE array here
void fun1() {
array[3][2] = 42.0;
}
int main(int argc,char *argv[])
{
int rows = atol(argv[1]);
int cols = atol(argv[2]);
//TO DO
//Allocate memory for array here
fun1();
printf("Array[3][2]=%f\n",array[3][2]);
return(0);
}
【问题讨论】:
-
您的问题标题为
at compile time C,但在帖子本身中您说的是allocate contiguous memory at runtime。那么...是哪一个? -
维度通过命令行参数传递,但数组必须声明为全局的。
-
那你需要在runtime分配;调用程序的命令行参数在编译时显然是未知的。请编辑问题以反映这一点。
-
我做了一些修改。我希望现在更清楚了。
标签: c dynamic allocation contiguous