Re: exercise on reinterpret_cast
"Igor Tandetnik" wrote:
Fil <Fil@discussions.microsoft.com> wrote:
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
Why? In the example above, you had to write p=&a, not p=a, and
apparently didn't find it the least bit surprising. So why do you have a
problem with p=&iA ?
Because iA should already contain the address of the first element of the
Array (like a fixed pointer).
Ex:
------------------------------------------------------------------
#include <iostream>
int main()
{
int a[5];
std::cout << a << " " << &a[0] << std::endl;
}
------------------------------------------------------------------
So if I am writing &iA it sounds to me like address of address of ...
This was confusing to me.
While I am talking I also tried to display for the first time &a and it
actually gives the same address than the 2 others. Waou.
iA is an address and I never asked to have it stored anywhere
I don't understand this at all. iA is an array. There's storage
allocated for this array. This storage begins at some address. &iA takes
this address.
(I mean
that an address of a pointer makes sense if the pointer has been
created.
First, iA is not a pointer, it's an array. Second, what you mean by
"created" here?
What also works is :
p=reinterpret_cast<double(*)[10]>(iA);
Is this the right way to do?
No, p=&iA is the right way to go (though declaring a pointer to an
array, as in double (*p)[10], is an unusual thing to want to do in the
first place).
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]'
That's pretty self-explanatory, isn't it?
This difference let me think that iA is more than &iA[0].
It is. Write a program that prints sizeof(iA) and sizeof(&iA[0]).
I maybe need to read something more thourough on this.
Can you please shed some light on the true nature of Arrays
http://www.transcendentaxis.com/dthompson/blog/archives/9
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925