There is a structure

struct Array { int **m; int sizeX; int sizeY; }; 

There is a function

 void input(Array *a) { for (int i = 0; i < a->sizeX; i++) for (int j = 0; j < a->sizeY; j++) { cout << "Input [" << i + 1 << ":" << j + 1 << "] element: "; cin >> a->m[i][j]; } for (int i = 0; i < a->sizeX; i++) { for (int j = 0; j < a->sizeY; j++) cout << a->m[i][j]; cout << endl; } } 

And there is a problem: when I launch the program, an error crashes. The MVS15 debugger says that the problem is in the structure element m
Here is the main function body

 int main() { Array *a = new Array; a->sizeX = 3; a->sizeY = 3; input(a); system("pause"); return 0; } 

When starting, the debugger says that m = 0xCEDECEDF and the program hangs
Where am I wrong?

    2 answers 2

    The pointer m not initialized, and in the input function you refer to this variable.

    Try this:

     Array *a = new Array; a->sizeX = 3; a->sizeY = 3; // Инициализация двумерного массива. a->m = new int*[a->sizeX]; for (int i = 0; i < a->sizeX; ++i) { a->m[i] = new int[a->sizeY]; } input(a); 

    And do not forget to release the dynamically allocated memory.

      The m pointer is not assigned a value anywhere.