tags:

views:

378

answers:

4

Hi

What is the C equivalent for reinterpret_cast?

Thanks

+5  A: 

C-style casts just look like type names in parenthesis:

void *p = NULL;
int i = (int)p; // now i is most likely 0

Obviously there are better uses for casts than this, but that's the basic syntax.

Chris Lutz
+2  A: 

A c-style cast:

int* two = ...;
pointerToOne* one = (pointerToOne*)two;
Reed Copsey
shouldn't that '==' be a single '=''?
KPexEA
Both are valid statements, and both will compile only because of the cast. Of course, if you want something to happen other than compiling, you'd use '='
MSalters
A: 

You can freely cast pointer types in C as you would any other type.

To be complete:

void *foo;
some_custom_t *bar;
other_custom_t *baz;
/* Initialization... */
foo = (void *)bar;
bar = (some_custom_t *)baz;
baz = (other_custom_t *)foo;
Sufian
You can freely cast any type, it just might not do what you hope it does, and might not be guaranteed to do anything at all.
Chris Lutz
+9  A: 
int *foo;
float *bar;

// c++ style:
foo = reinterpret_cast< int * >(bar);

// c style:
foo = (int *)(bar);
Crashworks
Please also note litb's very good point in the comments to the original question.
Crashworks