friends I was just playing around with some pointer programs and realized that the GCC (and maybe the C standard) differentiates the static and dynamic arrays.
A dynamic array has a place-holder for the address of the elements in the array whereas for the static array, there is no memory location where the compiler stores the starting address of the element array.
I have an example program to demonstrate my confusion.
#include <iostream>
#int main(void)
{
int _static[10];
int *_dynamic;
_dynamic = new int [10];
std::cout<<"_static="<<_static<<" &_static="<<&_static<<" &_static[0]="<<&_static[0]<<std::endl;
std::cout<<"_dynamic="<<_dynamic<<" &_dynamic="<<&_dynamic<<" &_dynamic[0]="<<&_dynamic[0]<<std::endl;
return 0;
}
For the above program, _static
and &_static[0]
return the same address on the expected lines. However, the &_static
also return the same address as the other two.
So, _static
and &_static
refer to the same number (or address whatever we would like to call it). As expected, _dynamic
and &_dynamic
indicate different locations.
So, why did the C standard say that _static
and &_static
must refer to the same location. It sounds confusing. One reason I feel is that &_static
does not make much sense. But then should its usage not be reported as an error instead ?
Can somebody please help me with this confusion ?
Actually _static
and &_static
don't refer to the same location. The only reason they appear to is because you use _static
in a context in which it decayed into a pointer. That is, by the way you used them, you made them refer to the same location. But they didn't before you did that -- one was an array and the other was a pointer. They couldn't be the same because they were fundamentally different things.