Codes are as follows:
#include <iostream>
using std::cout;
int main(int argc, char ** argv)
{
int** p;
int a[2][3]={{-1,-1,-1},
{-1,-1,-1}};
int k=1;
p = new int *[2];
for(int i=0;i<2;i++) {
p[i] = new int[3];
for(int j=0;j<3;j++) {
p[i][j] = k++;
}
}
memcpy(a,p,2 * 3 * sizeof(int));
for(int i=0;i<2;i++){
for(int j=0;j<3;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
delete [] p;
return 0;
}
Got unexpected results after running:
26573888 0 26573920
0 0 0
I think it copied addresses rather than values, so I changed the line memcpy(a,p,2 * 3 * sizeof(int));
to
memcpy(a,p[0],2 * 3 * sizeof(int));
And terminal printed out:
1 2 3
0 0 0
How to understand above results? Can anyone help me out?
GUESS
addresses of p[0][2]
and p[1][0]
are not continuous?