Type casting of pointer in C++ Programming Language

Author: Al-mamun Sarkar Date: 2020-04-03 16:55:12

Type casting of pointer in C++ Programming Language. The following code shows how to cast type of pointer in C plus plus Programming Language.

 

Code:

#include<iostream>
using namespace std;

int main()
{
    int a = 1025;
    int *p = &a;
    // Print Pointer Location and Value
    cout << "Location of pointer p is: " << p <<endl;
    cout << "Value of pointer p is: " << *p <<endl;


    char *c = (char*)&a;
    // Print Pointer Location and Value
    cout << "Location of pointer c is: " << c <<endl;
    cout << "Value of pointer c is: " << *c <<endl;


    return 0;
}

 

Output:

Location of pointer p is: 0x7ffee2d92a68
Value of pointer p is: 1025
Location of pointer c is: 
Value of pointer c is: