1

I am a newbie in C++ and it is only for learning purposes.

I just understood how to cast int input [][] to int** output with an auxiliary int* aux [] as follows.

int TwoD()
{
    int input[][3] = { {1,2,3},{4,5,6} };

    int* aux[2];

    aux[0] = input[0];// array of int ---> pointer to int
    aux[1] = input[1];// array of int ---> pointer to int

    int** output = aux;// array of int* ---> pointer to int*

    for (int i = 0; i < 2; i++)
        for (int j = 0; j < 3; j++)
            cout << output[i][j] << endl;
}

Now I want to extend it to 3D as follows.

void ThreeD()
{
    int input[2][3][4] =
    {
        {
            {1,2,3,4},
            {5,6,7,8},
            {9,10,11,12}
        },
        {
            {13,14,15,16},
            {17,18,19,20},
            {21,22,23,24}
        }
    };

    //int(*output)[3][4] = input;

    int** aux[2];
    aux[0][0] = input[0][0];
    aux[0][1] = input[0][1];
    aux[0][2] = input[0][2];
    aux[1][0] = input[1][0];
    aux[1][1] = input[1][1];
    aux[1][2] = input[1][2];

    int*** output = aux;

    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            for (int k = 0; k < 4; k++)
                cout << output[i][j][k] << " ";
            cout << endl;
        }
        cout << endl;
    }
}

It compiles but only produce a blank screen. What is the correct auxiliary aux and how to initialize it?

Second Person Shooter
  • 14,188
  • 21
  • 90
  • 165
  • Since `aux` doesn't point to anything, `aux[0]` is undefined behavior. – François Andrieux Jan 10 '19 at 19:54
  • Why are you interested in converting your array to a `int***`? – François Andrieux Jan 10 '19 at 19:55
  • 7
    `int***` is such obvious code smell, a sign of bad code, that it's become a joke. It makes you a [three star programmer](http://wiki.c2.com/?ThreeStarProgrammer). – François Andrieux Jan 10 '19 at 19:55
  • Are there awards for the most consecutive \*'s in a program? Otherwise, what's int*** supposed to do? – rsjaffe Jan 10 '19 at 19:55
  • @FrançoisAndrieux: It is just for learning purposes. I am looking for the pattern. – Second Person Shooter Jan 10 '19 at 19:55
  • Why are you learning C if you intend to learn C++? The `[ ]` stuff has so much exceptions it should be much later on the program – JVApen Jan 10 '19 at 19:55
  • jokes aside, here's a simple app to help you understand some gibberish: https://cdecl.org/. see, type of aux is now: "array 2 of pointer to pointer to int" so the way you populate it is wrong, for example – Hayri Uğur Koltuk Jan 10 '19 at 19:57
  • 2
    [Arrays decay to pointers](https://stackoverflow.com/questions/1461432/what-is-array-decaying). Multi-dimensional arrays decay to pointers to arrays, not pointers to pointers. In general, arrays are annoying to pass around. It's best to not use them. Prefer library containers like `std::vector` and `std::array`. – user4581301 Jan 10 '19 at 20:02
  • @user4581301: what type should the `aux` be? Give me a hint. – Second Person Shooter Jan 10 '19 at 20:03
  • `std::vector>>`, or if the size for each dimension is fixed, use `std::array, Y>, Z>` – SwiftMango Jan 10 '19 at 20:08
  • The accepted answer is what I am looking for. Now I am ready to proceed to become a C++ master. :-) – Second Person Shooter Jan 10 '19 at 20:11
  • 1
    @GodMustBeCrazy Heh. I resisted the urge to state that by the time I could make a proper answer explaining what was going on and how to avoid it, someone would have posted one much more succinct. Glad to see I was right. – user4581301 Jan 10 '19 at 20:38

1 Answers1

2

You need another layer of pointers.

int input[2][3][4] =
{
   {
      {1,2,3,4},
      {5,6,7,8},
      {9,10,11,12}
   },
   {
      {13,14,15,16},
      {17,18,19,20},
      {21,22,23,24}
   }
};

int* aux1[2][3] =
{
   { input[0][0], input[0][1], input[0][2] },
   { input[1][0], input[1][1], input[1][2] },
};

int** aux2[2] = {aux1[0], aux1[1]};

int*** output = aux2;
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • I love your explanation. It is very clear and direct to the problem. – Second Person Shooter Jan 10 '19 at 20:08
  • 1
    @GodMustBeCrazy, glad to hear it was useful :) – R Sahu Jan 10 '19 at 20:09
  • Sorry, I have a question about function pointer. If we pass a callback `bool isOdd (int x){return x%2==1;}` as an argument of another function with a signature `PrintList(bool (*f)(int),....other parameters...)`, which is the correct invocation ?`PrintList(isOdd,....);` or `PrintList(&isOdd,...);`? – Second Person Shooter Jan 11 '19 at 21:08
  • @GodMustBeCrazy, both are correct. Non-member functions are special in that regard. – R Sahu Jan 11 '19 at 21:17