C++ reinterpret_cast Keyword
Example
Convert a pointer to a number, and back to a pointer again:
int myNum = 10;
uintptr_t address = reinterpret_cast<uintptr_t>(&myNum);
int* ptr = reinterpret_cast<int*>(address);
cout << *ptr;
Definition and Usage
The reinterpret_cast keyword is a low-level conversion. It converts a pointer to an unrelated type, like another pointer type or a whole number. C++ does not check if the result makes sense.
In the example above, the address of myNum (where it is stored in memory) is converted to a number of type uintptr_t, from the <cstdint> header. Then the number is converted back to an int pointer. The pointer still points to myNum, so *ptr is 10.
Only use reinterpret_cast when you really need it, for example in programs that work directly with hardware. For normal conversions, like a double to an int, use static_cast.
Related Pages
Read more about pointers in our C++ Pointers Tutorial.
Read more about type conversion in our C++ Type Conversion Tutorial.