reinterpret_cast in C++ | Type Casting operators (original) (raw)
Last Updated : 13 Jun, 2026
reinterpret_cast is a C++ type casting operator used to reinterpret the bit pattern of an object as a different type. It provides a low-level conversion mechanism and offers very little type safety.
- Can convert pointers, references, and integral types to unrelated types.
- Performs no runtime type checking and preserves the underlying binary representation. CPP `
#include using namespace std;
int main() { int* p = new int(65); char* ch = reinterpret_cast<char*>(p); cout << *p << endl; cout << *ch << endl; cout << p << endl; cout << ch << endl; return 0; }
`
**Explanation
- p points to an integer with value 65.
- reinterpret_cast<char*>(p) treats the same memory address as a char*.
- *p prints the integer value 65.
- *ch reads the first byte of that memory, which may correspond to the character 'A'.
- p prints the memory address, while ch treats that address as a character sequence.
**Note: The output may vary across systems because reinterpret_cast depends on the underlying memory representation.
Syntax
data_type *var_name = reinterpret_cast <data_type *>(pointer_variable);
**Parameters
- **new_type: The target type to which the expression is converted.
- **expression: The value, pointer, or reference being reinterpreted.
**Return Value: Returns the expression interpreted as the specified type.
Common Uses of reinterpret_cast
reinterpret_cast is a low-level type conversion operator that allows one type to be treated as another without changing the underlying data. It is typically used in situations where direct access to memory representation is required.
- Converting pointers between unrelated types.
- Accessing the raw memory representation of an object.
- Performing low-level memory manipulation and bit-level operations.
- Interfacing with hardware, operating systems, or legacy APIs.
- Converting pointers to integer types and vice versa.
Since reinterpret_cast performs no runtime type checking and provides very little type safety, incorrect usage can lead to undefined behavior. It should be used only when other casting operators such as static_cast, dynamic_cast, or const_cast are not suitable.
Example: Accessing Object Memory
CPP `
// CPP code to illustrate using structure #include <bits/stdc++.h> using namespace std;
// creating structure mystruct struct mystruct { int x; int y; char c; bool b; };
int main() { mystruct s;
// Assigning values
s.x = 5;
s.y = 10;
s.c = 'a';
s.b = true;
// data type must be same during casting
// as that of original
// converting the pointer of 's' to,
// pointer of int type in 'p'.
int* p = reinterpret_cast<int*>(&s);
cout << sizeof(s) << endl;
// printing the value currently pointed by *p
cout << *p << endl;
// incrementing the pointer by 1
p++;
// printing the next integer value
cout << *p << endl;
p++;
// we are casting back char * pointed
// by p using char *ch.
char* ch = reinterpret_cast<char*>(p);
// printing the character value
// pointed by (*ch)
cout << *ch << endl;
ch++;
/* since, (*ch) now points to boolean value,
so it is required to access the value using
same type conversion.so, we have used
data type of *n to be bool. */
bool* n = reinterpret_cast<bool*>(ch);
cout << *n << endl;
// we can also use this line of code to
// print the value pointed by (*ch).
cout << *(reinterpret_cast<bool*>(ch));
return 0;}
`
Explanation
- The address of the structure object is reinterpreted as an int*.
- The first two integer members (x and y) are accessed through the converted pointer.
- The pointer is then reinterpreted as a char* to access the character member.
- Finally, the address is reinterpreted as a bool* to access the boolean member.
- This demonstrates how reinterpret_cast can be used to view the same memory through different pointer types.