Array of pointers to arrays of ints?

I have an array that I want to put a bunch of arrays into. I made the big array an array of type int * so it could point to the other arrays, like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int map0[5][5] = {
{0, 0, 0, 0, 0},
{0, 1, 1, 1, 0},
{0, 1, 1, 1, 0},
{0, 1, 1, 1, 0},
{0, 0, 1, 0, 0}};

int map1[5][5] = {
{0, 0, 1, 0, 0},
{0, 1, 1, 1, 0},
{1, 1, 0, 1, 1},
{0, 1, 1, 1, 0},
{0, 0, 1, 0, 0}};


int * world[5][5] = {
{0, 0, *map0, 0, 0},
{0, 0, *map1, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0}};


Then, later on, I have code to scroll through each map and check to see if the player has hit a border. If he hits a border, the player moves to the next array in the world array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
void gameloop()
{
	int x = 2;
	int y = 2;
	int i = 2;
	int j = 0;
	int ** mapl = &world[i][j];
	bool bGameOver = false;
	bool bChange = false;

	int * pPos = &(*mapl)[x][y]; //pointer pPos points to x y location in map
	int save = *pPos; //variable save is equal to the value pointed to by pPos which is map which is 1
	*pPos = 8; //the location pointed to by pPos is now 8; the x y loc. contains the value 8
	string sCom;

	printboard();

	while (!bGameOver){
		bChange = false;
		tprint("Use the keyboard to move by typing w, a, s, or d. Press x to end the game. ", n);

swLoop:
		*pPos = save; //value that pPos points to is now equal to save (pPos = 1) and 8 is still stored at x y loc. in map
		
		char ch = getch();
		
		if (ch == 'w'){ 
			if (mapl[x-1][y] == 0){
				tprint("You can't move into a 0 block. Try again: ", n);
				goto swLoop;
			}
			if (x-1 == -1){
				i -= 1;
				bChange = true;
			}
			pPos = &mapl[x-=1][y]; //pPos now points to a different position
			//x -= 1;
		}

		else if (ch == 's'){
			if (mapl[x+1][y] == 0){
				tprint("You can't move into a 0 block. Try again: ", n);
				goto swLoop;
			}
			if (x+1 == 5){
				i += 1;
				bChange = true;
			}
			pPos = &mapl[x+=1][y];
			//x += 1;
		}

		else if (ch == 'a'){
			if (mapl[x][y-1] == 0){
				tprint("You can't move into a 0 block. Try again: ", n);
				goto swLoop;
			}
			if (y - 1 == -1){
				j -= 1;
				bChange = true;
			}
			pPos = &mapl[x][y-=1];
			//y -= 1;
		}

		else if (ch == 'd'){
			if (mapl[x][y+1] == 0){
				tprint("You can't move into a 0 block. Try again: ", n);
				goto swLoop;
			}
			if (y + 1 == 5){
				j += 1;
				bChange = true;
			}
			pPos = &mapl[x][y+=1];
			//y += 1;
		}

		else if (ch == 'x'){
			exit(0);
		}
		else {
			tprint("Unable to recognize command, please try again: ", n);
			goto swLoop;
		}

		if (bChange == true){
			cout << "map changed";
		}

		system("CLS");

		save = *pPos; //save is now equal to the value pointed to by pPos which is 1
		*pPos = 8; // x y loc at new pPos location now contains 8

		printboard();

		cout << "Player " << *pPos << " at position (" << x << "," << y << ")\n";
	}
	system("PAUSE");
}


I'm getting errors at all of the [y]s in the old arrays. I also have an error at int ** mapl = &world[i][j]. I really don't know what this code says anymore. Can someone help me get this working?

EDIT: Actually, I think the problems may lie in the first bit of code I posted or in the lines with int ** mapl = &world[i][j]; and/or int * pPos = &(*mapl)[x][y];. Everything else worked before I introduced the master array (world).
Last edited on
Multidimensional arrays are evil.
Now you have a mess of pointers to pointers that don't really point to pointers.
Change your code to work with int map0[] = {...} and int* world[] = {...}. Then change map[i][j] to map[i+j*5]. It should make things easier to understand.
I did that, but I still have the same problem with the arrays and pointers...
Topic archived. No new replies allowed.