阅读这个问题:What is the difference between int* x[n][m] and int (*x) [n][m]?
正确的回复是这样的:
int arr[2][3];
int (*p1)[2][3] = &arr; // arr decays to int(*)[3], so we need to take the address
int (*p2)[2][3] = new int[42][2][3]; // allocate 42 arrays dynamically
最后一行的注释说它分配了 42 个新数组。 这是如何运作的?谁能给我解释一下?
谢谢!
我会回答那个问题,但我不能,所以这是个新问题 (:
最佳答案
new int[3]
分配 3 个整数。现在 new int[2][3]
分配了 2 个数组,每个数组的长度为 3 个整数。进一步扩展,new int[42][2][2]
分配了 42 个数组,每个数组的长度为 2 个数组,每个数组依次为长度为 2 个整数。声明实际上是一种递归思想。
关于c++ - 指向二维数组和分配的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22941030/