Using the C++ reinterpret_cast, to re-interpret an int to unsigned int pointer

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info:

To do: Re-interpreting an integer data type to an unsigned integer pointer using the C++ reinterpret_cast

To show: Using the C++ reinterpret_cast, re-interpreting an int to unsigned int pointer in C++ programming

 

// Using reinterpret_cast, int to unsigned int pointers conversion

#include <iostream>

using namespace std;

 

unsigned int* Test(int *q)

{

// convert int pointer to unsigned int pointer

unsigned int* code = reinterpret_cast<unsigned>(q);

// return the converted type data, a pointer...

return code;

}

 

int main(void)

{

// array name is a pointer...

int a[10];

 

// Take note that the output don't have obvious difference

cout<<"int pointer unsigned int pointer"<<endl;

for(int i = 0;i<=10;i++)

cout<<(a+i)<<" converted to "<<Test(a+i)<<endl;

 

return 0;

}

 

Output example:

 

int pointer unsigned int pointer

0012FF3C converted to 0012FF3C

0012FF40 converted to 0012FF40

0012FF44 converted to 0012FF44

0012FF48 converted to 0012FF48

0012FF4C converted to 0012FF4C

0012FF50 converted to 0012FF50

0012FF54 converted to 0012FF54

0012FF58 converted to 0012FF58

0012FF5C converted to 0012FF5C

0012FF60 converted to 0012FF60

0012FF64 converted to 0012FF64

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index