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.

#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

**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

**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.

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