Why do you need a C-style cast for the following?
int* ptr = static_cast<int*>(0xff); // error: invalid static_cast from type 'int'
// to type 'int*'
int* ptr = (int*) 0xff; // ok.
static_cast
can only cast between two related types. An integer is not related to a pointer and vice versa, so you need to use reinterpret_cast
instead, which tells the compiler to reinterpret the bits of the integer as if they were a pointer (and vice versa):
int* ptr = reinterpret_cast<int*>(0xff);
Read the following for more details:
Type conversions
You need a C-style cast or directly the reinterpret_cast
it stands for when casting an integer to a pointer, because the standard says so for unrelated types.
The standard mandates those casts there, because
When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?
Regular cast vs. static_cast vs. dynamic_cast
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With