[英]How to pass multidimensional arrays of pointers in function parameter, and manipulate it?
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.在我想将它传递给 function 参数之前,它工作得很好。 I tried multiple parameters and arguments and this works:
我尝试了多个参数和 arguments 并且这有效:
#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.但是,如果我想创建第二个数组来引用与 myPointingArray 相同的数组,我就是做不到。 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 !
谢谢 !
Why don't you just assemble the array of pointers mySecondArray[3][3]
the same way you did in the main()
with myPointingArray
?为什么不像在
main()
中使用myPointingArray
一样组装指针数组mySecondArray[3][3]
?
They will both point to the same underlying object.它们都将指向相同的底层 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]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.