0

So I have an array of pointers that this defined like so :

    sf::Sprite * game[3][3] = {{nullptr, nullptr, nullptr},
                       {nullptr, nullptr, nullptr},
                       {nullptr, nullptr, nullptr}};

It was working perfectly until I wanted to pass it into a function argument. I tried multiple parameters and arguments and this works :

#include <iostream>

void printTwoDPointersArr(int * myArray[][3])
{
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            std::cout << *myArray[i][j];
}

int main()
{
    int myRealArray[3][3] = {{0, 1, 2}, {3, 4, 5}, {0, 1, 2}};
    int * myPointingArray[3][3];
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            myPointingArray[i][j] = &myRealArray[i][j];
    printTwoDPointersArr(&myPointingArray[0]);
}

But then, if I want to create an second array that refers to the same array than myPointingArray, i just can't. I don't want a copy of the array, because copies are not updated when the original one is changed : I need a reference. I tried this :

#include <iostream>

void printTwoDPointersArr(int * myArray[][3])
{
    int * mySecondArray = &myArray[0];
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            std::cout << *mySecondArray[i][j];
}

int main()
{
    int myRealArray[3][3] = {{0, 1, 2}, {3, 4, 5}, {0, 1, 2}};
    int * myPointingArray[3][3];
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            myPointingArray[i][j] = &myRealArray[i][j];
    printTwoDPointersArr(&myPointingArray[0]);
}

But I get the following error :

/path/main.cpp:5: error: cannot convert ‘int* (*)[3]’ to ‘int*’ in initialization
/path/main.cpp:5:27: error: cannot convert ‘int* (*)[3]’ to ‘int*’ in initialization
    5 |     int * mySecondArray = &myArray[0];
      |                           ^~~~~~~~~~~
      |                           |
      |                           int* (*)[3]

So, the question is : is there an easy way to manipulate multidimensional array of pointers, and to create a reference to them ?

I am new to stack overflow, please tell me if I'm doing wrong. Thank you !

Bebel
  • 49
  • 6
  • 1
    *I need a reference* -- Note -- Your usage of `&` does not mean "reference". It is the `address-of` operator, separate and apart from what C++ references are. – PaulMcKenzie Dec 13 '20 at 13:17
  • Consider using `using Array3x3 = std::array, 3>;` instead of raw arrays. Then it becomes much easier to pass `Array3x3` around than raw arrays, with the initialization simply having an extra set of braces: `Array3x3 myRealArray = {{{0, 1, 2}, {3, 4, 5}, {0, 1, 2}}};` – PaulMcKenzie Dec 13 '20 at 13:24
  • I know I should use std::array... But is it possible to do with basic arrays ? – Bebel Dec 13 '20 at 13:25
  • [How to pass 2D array as argument](/questions/4802674/how-to-pass-two-dimensional-array-as-an-argument) – PaulMcKenzie Dec 13 '20 at 13:37
  • @PaulMcKenzie Actually `std::array is a bit better - regarding initialization syntax. – Red.Wave Dec 13 '20 at 13:46

1 Answers1

0

Why don't you just assemble the array of pointers mySecondArray[3][3] the same way you did in the main() with myPointingArray?

They will both point to the same underlying object.

#include <iostream>

void printTwoDPointersArr(int * myArray[][3])
{
    int * mySecondArray[3][3];
    // now just assemble the array of pointers as done in main()
    for (int i = 0; i < 3; i++)      
        for (int j = 0; j < 3; j++)
            mySecondArray[i][j] = myArray[i][j];
            
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            std::cout << *mySecondArray[i][j];
}

int main()
{
    int myRealArray[3][3] = {{0, 1, 2}, {3, 4, 5}, {0, 1, 2}};
    int * myPointingArray[3][3];
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            myPointingArray[i][j] = &myRealArray[i][j];
    printTwoDPointersArr(&myPointingArray[0]);
}
Giogre
  • 1,444
  • 7
  • 19