Manipulating Data by Using Pointers - C++ Data Type

C++ examples for Data Type:Pointer

Introduction

The Pointer program demonstrates how the address of a local variable is assigned to a pointer and how the pointer manipulates the values in that variable.

Demo Code

#include <iostream> 
 
int main() //  www . jav a  2s .com
{ 
    int myAge;                  // a variable 
    int *pAge = nullptr;  // a pointer 
 
    myAge = 5; 
    pAge = &myAge;     // assign address of myAge to pAge 
    std::cout << "myAge: " << myAge << "\n"; 
    std::cout << "*pAge: " << *pAge << "\n\n"; 
 
    std::cout << "*pAge = 7\n"; 
    *pAge = 7;         // sets myAge to 7 
    std::cout << "*pAge: " << *pAge << "\n"; 
    std::cout << "myAge: " << myAge << "\n\n"; 
 
    std::cout << "myAge = 9\n"; 
    myAge = 9; 
    std::cout << "myAge: " << myAge << "\n"; 
    std::cout << "*pAge: " << *pAge << "\n"; 
 
    return 0; 
}

Result


Related Tutorials