【发布时间】:2018-07-26 14:31:27
【问题描述】:
我想知道为什么会这样
// read n as matrix dimension, then:
int* M;
M = (int*)malloc(n * n * sizeof(int));
// cycle through each "cell" in the matrix and read a number with
scanf("%d", &M[i * n + j]);
这不是吗?
// read n as matrix dimension, then:
int** M;
M = malloc(n * n * sizeof(int));
// cycle through each "cell" in the matrix and read a number with
scanf ("%d", &M[i][j]);
我只是不明白。在这两种情况下,它们都应该是双指针,对吗?
【问题讨论】:
-
你错了。 (好吧,如果你不是,你就不会问这个。)第二个是,不管它的尺寸如何,一个指向指针数组的指针。哪些尚未分配。
-
另见this方法:矩阵作为抽象数据类型。
标签: c pointers matrix dynamic allocation