3-D Arrays in C++

Posted on 2004-10-22
Last Modified: 2010-04-01
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)
Question by:VBStudent
    LVL 3

    Assisted Solution

    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);

    LVL 55

    Accepted Solution

    Have a look to a previous question where I implement a 3D class, you will notice similar calculations:

    Author Comment

    Thanks for the help and the prompt response.  I actually needed both solutions.  Thanks to both of you.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
    In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now