# Thread: Initializing a 3D array w/o a loop.

1. ## Initializing a 3D array w/o a loop.

What's the syntax? For example, a 2d array:
Code:
```int[][] array = {
{2, 5} , {7, 4},
};```
I'm looking for a similar approach with a 3D array but can't seem to find it anywhere. 2. same way, but it starts to get messy and hard to follow...
Code:
```int array =
{
{
{1,1},
{1,2}
},{
{2,1},
{2,2}
}
};``` 3. yeah 3d arrays are pain in the ars. What are you trying to do with them funkydude? 4. Originally posted by axon
yeah 3d arrays are pain in the ars. What are you trying to do with them funkydude?
I'm making Tetris, I need a 3d array to represent each block and it's rotations. I realize I could of just calculated the rotation myself using just a 2d array also, but I decided to go this way. 5. a 3d array is the hard way of going about it dude. Do it in 2d and figure out the rotation there. If you're having troubles do a search....this has been done many times here before. 6. Also, remember that in memory, the data is laid out just as it would be if it were a one-dimensional array. You can see this for yourself by simply casting the base of the array to a pointer of the appropriate type:

Code:
``` const int X = 4, Y = 2, Z = 5;

int abc[X][Y][Z] =
{ { { 1,  2,  3,  4,  5 }, { 6,  7,  8,  9, 10 } },
{ {11, 12, 13, 14, 15 }, {16, 17, 18, 19, 20 } },
{ {21, 22, 23, 24, 25 }, {26, 27, 28, 29, 30 } },
{ {31, 32, 33, 34, 35 }, {36, 37, 38, 39, 40 } } };

int main()
{
for(int x = 0; x < X; ++x)
for(int y = 0; y < Y; ++y)
for(int z = 0; z < Z; ++z)
printf("%d...", abc[x][y][z]);

const int U = X * Y * Z;

int * r = (int*)abc;

printf("\n\n - again - \n\n");

for(int u = 0; u < U; ++u)
printf("%d...", r[u]);

return 0;
}``` Popular pages Recent additions 