Displaying pointers variable data C++ code sample

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows 2003 Server Standard Edition

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: none

To do: Displaying pointers data in demonstrating how to use the pointers variable

To show: How to use the pointers variable in C++ programming

 

// a program to illustrate the basic use of pointers

#include <iostream>

using namespace std;

 

void main(void)

{

// declares an integer variable and two pointers variables

int num = 10, *point_one, *point_two;

 

// assigns the address of variable num to pointer point_one

point_one = &num;

 

// assigns the (address) point_one to point_two

point_two = point_one;

cout<<"Pointers variables..."<<endl;

cout<<"*point_one = "<<*point_one<<"\n";

cout<<"*point_two = "<<*point_two<<"\n";

cout<<"\nNormal variable..."<<endl;

cout<<"num = "<<num<<"\n";

// displays value 10 stored in variable num since point_one and point_two now point to variable num

cout<<"\n-Both pointer point_one and"<<"\n";

cout<<"-point_two point to the same variable num."<<"\n";

cout<<"-That is why, they have same value, 10."<<endl;

}

 

Output example:

 

Pointers variables...

*point_one = 10

*point_two = 10

Normal variable...

num = 10

-Both pointers point_one and

-point_two point to the same variable num.

-That is why, they have same value, 10.

Press any key to continue . . .

 

 

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