Cpp - Examining Addresses Stored in Pointers

Introduction

The following code shows the value of a pointer.

Demo

#include <iostream> 
 
int main() //from www.  j  a  v a 2s . c om
{ 
      int myAge = 5, yourAge = 10; 
      int *pAge = &myAge;  // a pointer 
 
      std::cout << "myAge:\t" << myAge; 
      std::cout << "\t\tyourAge:\t" << yourAge << "\n"; 
      std::cout << "&myAge:\t" << &myAge; 
      std::cout << "\t&yourAge:\t" << &yourAge <<"\n"; 
 
      std::cout << "pAge:\t" << pAge << "\n"; 
      std::cout << "*pAge:\t" << *pAge << "\n\n"; 
 
      pAge = &yourAge;            // reassign the pointer 
 
      std::cout << "myAge:\t" << myAge; 
      std::cout << "\t\tyourAge:\t" << yourAge << "\n"; 
      std::cout << "&myAge:\t" << &myAge; 
      std::cout << "\t&yourAge:\t" << &yourAge <<"\n"; 
 
      std::cout << "pAge:\t" << pAge << "\n"; 
      std::cout << "*pAge:\t" << *pAge << "\n\n"; 
 
      std::cout << "&pAge:\t" << &pAge << "\n"; 
      return 0; 
}

Result

Related Topic