RE: exercise on reinterpret_cast
Hi,
I was now trying to make a pointer to an array of 10 double, initialize it
and play a little bit tobecome confident.
That's what I wrote initially:
--------------------------------------------------------------------------------------
int main()
{
double iA[10];
double(*p)[10];
p=iA;
std::cout << sizeof(iA) << std::endl;
std::cout << (long)p << std::endl;
p++;
std::cout << (long)p << std::endl;
}
--------------------------------------------------------------------------------------
p is my pointer to a array of 10 double.
iA is my array of 10 double.
So I decide to have p pointing to iA.
Since I learnt that iA is the address of the first element of the array this
is the address I want p to contain.
So I write p=iA.
But it doesn't work.
I thought I was doing something as natural as:
int a;
int * p;
p=&a;
Instead it works if I write p=&iA which makes no sense to me because iA is
an address and I never asked to have it stored anywhere (I mean that an
address of a pointer makes sense if the pointer has been created. Here I just
have an expression that means, as far as I've learnt, the address
of(something representing already an address)).
What also works is :
p=reinterpret_cast<double(*)[10]>(iA);
Is this the right way to do?
static_cast doesn't work here: the compiler says:
cannot convert from 'double [10]' to 'double (*)[10]'
If I write ...
p=static_cast<double(*)[10]>(&iA[0]);
.... I got : cannot convert from 'double *' to 'double (*)[10]'
This difference let me think that iA is more than &iA[0].
I maybe need to read something more thourough on this.
Can you please shed some light on the true nature of Arrays
Well I am happy to see that sizeOf(iA) is indeed sizeOf(double)*10. That
gives me a little bit of confidence.