Hello! There was the following problem.

char **map = new char*[s]; //так я объявляю двумерный динамический массив for(int i = 0; i < s; i++) map[i] = new char[c]; 

Next, I initialize it. When you try to display it, everything runs smoothly.

 cout << map[i][j]; // так я вывожу каждый элемент массива(в цикле) 

But when you try to change the value of an array element, the program fails.

 map[x][y] = 'H'; // так я пытаюсь присвоить нужному элементу массива конкретное значение. 

Actually, here's the error: "Unhandled exception at 0x00fa16f1 in test.exe: 0xC0000005: Access violation writing location location 0x00fa89b5."

I guess that because pointers are used, it is impossible to directly change the value in the memory to which they are pointing, but I don’t know how to do it correctly.

    2 answers 2

    Based on your code made an example. Everything works as expected and does not fall.

     #include <iostream> using namespace std; int main() { int s = 5; int c = 4; char **map = new char*[s]; //так я объявляю двумерный динамический массив for(int i = 0; i < s; i++) map[i] = new char[c]; for (int i = 0; i < s; i++) for (int j = 0; j < c; j++) map[i][j] = i*j; for (int i = 0; i < s; i++) { for (int j = 0; j < c; j++) cout << (int)map[i][j] << " "; cout << endl; } // здесь ещё нужно вставить освобождение памяти for (int i = 0; i < s; i++) free(map[i]); free(map); return 0; } 

    If your code falls, give it completely (or collect a minimal example that also falls).

    • This code ends with the same error. #include <iostream> using namespace std; int main () {int s = 5; int c = 4; char ** map = new char * [s]; // so I declare a two-dimensional dynamic array for (int i = 0; i <s; i ++) map [i] = new char [c]; map [0] = "ooo"; map [1] = "ooo"; map [2] = "ooo"; map [3] = "ooo"; map [4] = "ooo"; map [0] [0] = 'a'; for (int i = 0; i <s; i ++) {for (int j = 0; j <c; j ++) cout << map [i] [j] << ""; cout << endl; } return 0; } - iterq
    • and so you can not write. Instead of map [0] = "ooo"; need to write strcpy (map [0], "ooo"); and do not forget to #include <cstring> Either do not bother yourself and use std::vector<string> . - KoVadim
    • And what did you want? First, initialize the map array, and then spoil it. @KoVadim you can write like that, you just need to understand what comes out of it ... - alexlz
    • @iterq, falls, because You are trying to modify constants. Instead of map [0] = "ooo"; write map [0] = strdup ("ooo"); but here you lose initialization on new char [c], although you already lose it. - avp
    • and get a memory leak :) - KoVadim

    Most likely, a non-existent cell is accessed (X db in the range (0..s], Y db in the range (0..c]).

    S - number of lines

    • I checked it, at the time of error the IDE pauses and you can see the values ​​of all variables. Everything is alright with them. The error occurs precisely in the case of using pointers to declare an array. If you declare as usual (char map [s] [c];), then everything works. - iterq
    • Try changing the array name from map to myMap ... Perhaps there is a name conflict (with std :: map) - AlekseyOk
    • @iterq, code in the studio. - avp
    • @avp, I gave a shorter code example in a comment to the KoVadim answer. It ends with the exact same error. - iterq