stuck with a trivia
-
It seems you're confusing two well distinct concepts (don't worry: it takes about three years to me to get away from that confusion...). Let's reconsider your points:
Smith# wrote:
As you know pointers & arrays both move consecutive
No. Pointers "move" (in th sense they can receive another address as their own stored value). Array just "are". Array indexes "move" (in the sense they can be used to indicate other array cells).
Smith# wrote:
how come it be totally different
Because they ARE totally different. They simple have a "similar" external interface (same operators).If you don't get this point may be I was not able to explain the first point, but until you don't get it you cannot move away from your trivia. So loop over and over until you get it, find other sources than me (if you find my way to explain is not clear) but there is no way to move over until this crucial point is clear.
Smith# wrote:
Because the next pointer in the **ptr can be made to point to some other *p pointing somewhere. But here that's not the case.. it's array,
You know, but compiler doesn't.
int**
for the compiler is just 4 bytes of memory containing the address of an int* that is another 4 byte of memory, somewhere else, containing the address of an int. Whatever you think about this is irrelevant to the compiler.int[]
is a consecutive bounce of integers at some place in memory. This place has an address. When you doint a[2] = { 0,1 };
int* pa = a;you assign the address of the array head to the pointer. When you do
*pa = 5;
the compiler assign
5
to the memory whose address is stored in "pa". This accidentally is a[0] (but it is not something the compiler is aware of). When you dopa[1] = 6;
the compiler assign 6 to the memory whose address is given by
pa+1*sizeof(int)
, that accidentally is a[1] (again you know, not the compiler: there is nothing in "pa" that makes the compiler aware of the fact that it is NOW pointing to an array and sometime later on to an individual integer and some later on to an integer inside a class or struct. The design of the [] operator appllied to poitner or to array makes this operation giving a same result just because for monidimensional array, the same calculation is done. Bidimensional arra