【发布时间】:2013-08-16 22:32:02
【问题描述】:
C++:我有一个指向 3D 数组的指针的问题 - 我正在编写一个带有 2D 数组的基本游戏,每个 2D 数组都是一个单独的关卡,这些关卡被分组到一个名为 map 的 3D 数组中。
如何指向我的游戏的每个“关卡”?我的精简代码:
#include<iostream>
using namespace std;
#define LEVEL 2
#define HEIGHT 3
#define WIDTH 3
bool map[LEVEL][HEIGHT][WIDTH] = { {{1, 0, 1},
{1, 0, 1},
{0, 0, 1}},
{{1, 1, 0},
{0, 0, 0},
{1, 0, 1}} };
int main()
{
// ideally this points to level#1, then increments to level#2
bool *ptrMap;
for(int i=0; i<HEIGHT; i++)
{
for(int j=0; j<WIDTH; j++)
cout << map[1][i][j]; // [*ptrMap][i][j] ?
cout << endl;
}
return 0;
}
【问题讨论】:
-
结构体与单色数组的组合更容易理解。
-
为什么需要指针?
-
不要使用宏,使用 const