3D arrays

Can anyone please tell me the method of declaring a 3D array ?
MyArray[][][]; Of course, you add the type of array in front of the declaration. That is: int, char, string etc.
Also, inside each set of brackets, put in your sizes.
Last edited on
and how can we take values in our 3D array from the user ?
I'd prefer to answer with "you don't - whatever you were trying to do with a 3D array can be done better without one" - actually, unless you're using C, you probably don't wanna use C style arrays at all - use std::vector and std::array instead. Still not for 3D arrays though.

For the sake of completeness, you can declare one like this:
 
int arr[L][W][H];


where L, W and H are compile time constants, or like this:
1
2
3
4
5
6
7
int*** arr = new int**[L];
for(int i=0; i< L; ++i) {
     arr[i] = new int*[W];
     for(int j=0; j<W; ++j) {
          arr[i][j] = new int[H];
     }
}


where L, W and H are (positive) integers (don't have to be constant).
Last edited on
ok..
thanks..
Topic archived. No new replies allowed.