私は、int型の3dマトリックスにスペースを動的に割り当てる方法をWebで探していました。そして、私は2次元行列に関する多くのサイトを見つけまし た.これはhttp://www.taranets.com/cgi/ts/1.37/ts.ws.pl?w=329;b=286 そして、下に示すようにこの例がありました. 上記の例はすべて理解しましたが、これは 3d に関するものではありません。作成者は逆方向にスペースを割り当てていますか、それとも他に何ですか? 彼はマトリックス全体にスペースを割り当てることから始めて、次に Z 軸に進みますか? それは私が理解できないことです。
また、これに関する良いサイトをご存知でしたら、ここに投稿していただければ幸いです。
/* Program 9.4 from PTRTUT10.HTM 6/13/97 */
// http://www.taranets.com/cgi/ts/1.37/ts.ws.pl?w=329;b=286
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
int X_DIM=16;
int Y_DIM=5;
int Z_DIM=3;
int main(void)
{
char *space;
char ***Arr3D;
int y, z;
ptrdiff_t diff;
/* first we set aside space for the array itself */
space = malloc(X_DIM * Y_DIM * Z_DIM * sizeof(char));
/* next we allocate space of an array of pointers, each
to eventually point to the first element of a
2 dimensional array of pointers to pointers */
Arr3D = malloc(Z_DIM * sizeof(char **));
/* and for each of these we assign a pointer to a newly
allocated array of pointers to a row */
for (z = 0; z < Z_DIM; z++)
{
Arr3D[z] = malloc(Y_DIM * sizeof(char *));
/* and for each space in this array we put a pointer to
the first element of each row in the array space
originally allocated */
for (y = 0; y < Y_DIM; y++)
{
Arr3D[z][y] = space + (z*(X_DIM * Y_DIM) + y*X_DIM);
}
}
/* And, now we check each address in our 3D array to see if
the indexing of the Arr3d pointer leads through in a
continuous manner */
for (z = 0; z < Z_DIM; z++)
{
printf("Location of array %d is %p\n", z, *Arr3D[z]);
for ( y = 0; y < Y_DIM; y++)
{
printf(" Array %d and Row %d starts at %p\n", z, y, Arr3D[z][y]);
diff = Arr3D[z][y] - space;
printf(" diff = %d ",diff);
printf(" z = %d y = %d", z, y);
}
putchar('\n');
}
putchar('\n');
system("PAUSE");
return 0;
}