### Pointer to a 2D array

I am still new to C++, please excuse any silly mistakes i make.
I'm trying to make a simple game where i use 2D arrays as maps.
To make multiple maps I'm trying create a pointer to a 2D array. This is an example of what I'm trying to do:
 ``12345678910111213141516171819202122232425262728`` ``````#include #include char * Map[20][20]; char map1 [20][20] = {"##################", "# # #", "# # !", "# # # #####", "# # # # #", "#@ # # #", "#### # #", "# ####### #", "# #", "##################"}; int main() { * Map[20][20] = map1[20][20]; system("cls"); for (int y = 0; y < 20; y++) { std::cout << Map[y] << "\n"; } return 0; }``````

This code compiles, but when run it crashes.
I tried looking this up on Google and forums, but all the questions i found involve matrices which is very confusing for me.
Any help would be appreciated :)
In line 4 change `char * Map[20][20];` to `char** Map;`
in line 19, change `* Map[20][20] = map1[20][20];` to `Map = map1;`

The fact that you have `char*` makes line 24 work, but if you had used any other type, you'd need to use a nested loop to print each element of the array:
 ``123456`` ``````for (int y = 0; y < 20; y++) { for (int x = 0; x < 20; x++) std::cout << Map[y][x]; std::cout << std::endl; }``````
`char * Map[20][20];`
This creates an array of 20x20 char pointers.

To create a pointer to a 20x20 char array you will have to put parantheses around the array name and the asterix.
`char (* Map)[20][20];`

This line
`* Map[20][20] = map1[20][20];`
should be
`Map = &map1;`

Before you use the array that the pointer points to you have to dereference the pointer with the * operator.
 ``1234`` ``````for (int y = 0; y < 20; y++) { std::cout << (*Map)[y] << "\n"; }``````

Last edited on
This worked! :)
Thank you very much guys for your quick responses
Topic archived. No new replies allowed.