-2

C++: 3D 配列へのポインターに問題があります。2D 配列を使用して基本的なゲームを作成しています。各 2D 配列は個別のレベルであり、これらのレベルはマップと呼ばれる 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;    
}
4

2 に答える 2

0

割当、

bool *ptrmap= &map[0][0][0];// gives you level 0
cout<<*ptrmap; //outputs the first element, level 0
cout<<*(ptrmap+9);// outputs first element, level 1

ポインタの線形インクリメントが必要ない場合は、

i.e. map[0][0][0] as *ptrmap & map[1][0][0] as *(ptrmap + 9)

、ポインターのポインターを使用してマトリックスを作成し(例:bool ***ptrmap)、一時ポインターを作成して逆参照することをお勧めします。

于 2013-08-17T05:46:05.420 に答える