I successfully entered values to a 2D array without pointers
int main(){
int A[2][3];
for (int i=0; i<2; i++){
for(int j=0; j<3; j++){
A[i][j] = 2*i+3*j;
cout<<" "<<A[i][j]<<" ";
}
cout<<endl;
}
}
And the output is
0 3 6
2 5 8
Then I tried to reach the same outcome with pointers
int main(){
int A[2][3];
int (*p)[3] = A;
for (int i=0; i<2; i++){
for(int j=0; j<3; j++){
*(*A+j)= 2*i+3*j;
cout<<" "<<*(A[i]+j)<<" ";
}
cout<<endl;
}
}
And the output is
0 3 6
32758 1 0
any idea why I got a different result for the second array?
p, you are still usingA. And of course*(*A+j)is obviously not the same asA[i][j], there's no use ofiin the first expression for instance.[]works perfectly well with pointers. So the correct pointer using code would bep[i][j] = 2*i+3*j;