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 !