views:

444

answers:

5

When I try to use a static_cast to cast a double* to an int*, I get the following error:

invalid static_cast from type ‘double*’ to type ‘int*’

Here is the code:

#include <iostream>
int main()
{
        double* p = new double(2);
        int* r;

        r=static_cast<int*>(p);

        std::cout << *r << std::endl;
}

I understand that there would be problems converting between a double and an int, but why is there a problem converting between a double* and an int*?

A: 

Because you used double * instead of double

The * after it means that you are declaring a pointer, which is vastly different from a regular double.

C++ can not safely static_cast a pointer to a different type of pointer like that.

If you are wanting to do this kinda thing, you must first dereference the variable.

r=new int(static_cast<int>(*p));

You must use new because a double and an integer can not reside in the same memory space(sanely)

Earlz
+10  A: 

You should use reinterpret_cast for casting pointers, i.e.

r = reinterpret_cast<int*>(p);

Of course this makes no sense,

unless you want take a int-level look at a double! You'll get some weird output and I don't think this is what you intended. If you want to cast the value pointed to by p to an int then,

*r = static_cast<int>(*p);

Also, r is not allocated so you can do one of the following:

int *r = new int(0);
*r = static_cast<int>(*p);
std::cout << *r << std::endl;

Or

int r = 0;
r = static_cast<int>(*p);
std::cout << r << std::endl;
Jacob
"… should use reinterpret_cast …" is not a good phrase.
Potatoswatter
More like you /need/ to use reinterpret_cast for pointers... the only valid scenario to use it though is if you have (mostly for technical reasons) a void* which need to cast to a concrete type.e.g. 3rd parameter of pthread_create() is a function which has 1 argument: a void* which you need to then cast to your structure/etc.
Serguei
The world isn't perfect. You're forced to use code from other libraries. And **in that situation** "use reinterpret_cast" **is** a good phrase :)
Jacob
There are other cases - for example in OpenCV the images are padded so to traverse `double` images using pointer arithmetic, you need to work at the level of an `unsigned char` to skip the padding to move between rows. So, then accessing the `double` values makes sense if you use `reinterpret_cast`. That's the **only** place I've used them but it has made my code much cleaner.
Jacob
"put some information into p before using it!"...That's what new double(2) does, it initializes the double value to 2.
SoapBox
Sorry, thought I saw it was `[]`, I've fixed it.
Jacob
Found more errors in the code ...
Jacob
What's so bad about `r` being unitialized? It's not read before being assigned.
zneak
I mean **allocated** - sheesh, not a good day today.
Jacob
+11  A: 

Aside from being pointers, double* and int* have nothing in common. You could say the same thing for Foo* and Bar* pointer types to any dissimilar structures.

static_cast means that a pointer of the source type can be used as a pointer of the destination type, which requires a subtype relationship.

Potatoswatter
A: 

You can convert between a double and an int with static_cast<>, but not between pointers to different types. You can convert any pointer type to or from void * with static_cast<>.

The rationale may be that int * and double * are often effectively arrays, and the implementation doesn't know how big the array is.

David Thornley
+4  A: 

Floating point-to-integer conversion is supported, so int a = static_cast<int>(5.2) is fine. However, it's a conversion - the underlying data types are completely incompatible. What you're asking is for the runtime to convert a pointer to an 8-byte structure to a pointer to a 4-byte structure, which it can't do in any meaningful way.

That having been said, if you really want to interpret your double as an integer, int* r = reinterpret_cast<int*>(p) will work fine.

Dathan