To do so, we start by allocating space for all array elements in one call to malloc.
int
*allElements =
malloc
(x * y * z *
sizeof
(
int
));
Next, we create the arrays of pointers, and point to the contiguous elements we’ve already allocated.
int
***array3D =
malloc
(x *
sizeof
(
int
**));
for
(i = 0; i < x; i++)
{
array3D[i] =
malloc
(y *
sizeof
(
int
*));
for
(j = 0; j < y; j++)
{
array3D[i][j] = allElements + (i * y * z) + (j * z);
}
}
No comments:
Post a Comment