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 ?