Lately I started learning pointers and references in c++ (not just the usual use of them,but all kinds of ways,I don't want to have problems with them in near future).
From my perspective a 1d static allocated array is similar to a int* const pointer. My problem is that ,when I allocate dynamically with a pointer, the pointer itself has other memory address than the first element,which is not the case in a 1d array. This is an example
int a[5];
cout<<&a<<" "<<&a[0]<<"\n";
int* t=new int[10];
cout<<&t<<" "<<&t[0]<<"\n";
The output is:
0x6afed0 0x6afed0
0x6afecc 0xcb0e40
I can't think how the 1d array is stored,at 1 block of 4 bytes you can store either a value or an address. I tried also with 2d arrays,and there are more intersections.
q = new int*[10];
for (i=0;i<10;i++)
q[i] = new int[i+1];
cout<<&q<<" "<<&q[0]<<" "<<&q[0][0]<<"\n";
int b[10][10];
cout<<&b<<" "<<&b[0]<<" "<<&b[0][0]<<"\n";
Output:
0x6afee4 0xe40e40 0xe41240
0x6afd54 0x6afd54 0x6afd54
If someone would enlighten me and explain how these arrays are stored,I would be thankful.
EDIT:To be more specific, I know and it's logical how the array dynamically created with new keyword by the pointer is stored. But how &a and &a[0] have the same values,if a should store the address of a[0] .That block of memory(&a) stores what then?the value of a[0] or the address of a[0]. Hope I was clear in what my problem is.