Cpp - Examining Addresses Stored in Pointers

Introduction

The following code shows the value of a pointer.

Demo

#include <iostream> 
 
int main() //from w  ww.j av  a 2 s .c o  m
{ 
      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