# 3-D Arrays in C++

I'm trying to recall how to build a 3-D array in C++.  I know that 2-D arrays can be declared as int myArray[#rows][#cols] but if I remember correctly, doesn't a 3-D have to be constructed with new int** filling each single dimension array then adding the next dimension or layer on top of it?  Along those same lines, after the array is constructed, what is the order or dimension that the brackets refer to?  ie.  myArray[#deep][#rows][#cols] ?? isn't that the most widely accepted abstracted view?  Elaborating a little bit more, to calculate the address of an element in integer myArray[2][3][4] in myArray with the following dimensions myArray[5][6][7]  the calculation would be something like this address = base + ((2*6+4) * 7 + 3) * (sizeof int)
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
1. You may still declare it as:
int myArray[5][5][5];

2. You may call the dimensions whatever you want, ie. rows, colls, deep or year, month, day etc.
you reference the elements in the same way it was declared.  int myArray[1st-dim][2nd-dim][3rd-dim];

3. You don't have to "calculate" the address of course, just use &(myArray[2][3][4]);
...but, in your example it would be:
address = base + ((2 * (6 + 7)) + (3 * 6) + 4) * sizeof(int);

Regards
Hendrik
0
Software ArchitectCommented:
Have a look to a previous question where I implement a 3D class, you will notice similar calculations:
http://www.experts-exchange.com/Q_21021623.html
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Thanks for the help and the prompt response.  I actually needed both solutions.  Thanks to both of you.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.