...
//initializing first 2D dynamic array
int x=2;
int y=2;
int** array = new int*[x];
for(int i=0; i<x; i++) array[i] = new int[y];
//initializing second 2D dynamic array
int new_x=3;
int new_y=3;
int** temp = new int*[new_x];
for(int i=0; i<new_x; i++) temp[i] = new int[new_y];
//assigning values
array[0][0] = 1;
cout << array[0][0] << endl; //output is 1
//before assigning values to temp[0][0]
cout << temp[0][0] << endl; //out is a huge random number
temp[0][0] = array[0][0];
cout << temp[0][0] << endl; //output is the same huge random number
temp[0][0] = 2;
cout << temp[0][0] << endl; //output is 2
...
[第一个cout
打印1
的值是array[0][0]
的值。
[第二cout
打印未初始化的temp[0][0]
,我将其编译为here,在这种情况下,值为0
,但可以是任何值。
由于cout
的缘故,第三1
打印temp[0][0] = array[0][0]
。
第四cout
打印分配给2
的temp[0][0]
的值。
如果您的代码中有问题,除了您试图在第二个cout
中打印一个单位化变量的事实,它不在发布的位中。>