/**************************************************************/
// R. A. Hillyard
// pointer00.cpp
// November 2001
//
// Program to demonstrate the basic use of pointers
/**************************************************************/
#include<iostream>
#include<iomanip>
#include<cstddef> //for definition of NULL
using namespace std;
int main()
{
int a = 7; //declare an int - set value to 7
int b = 0; //declare an int - set to 0;
int *ptr = NULL; //declare an int pointer set to NULL
//show the initial value and address of each variable.
cout << "\nInitial values a: " << a << " b: "<< b;
cout << " ptr: " << ptr << endl;
cout << "\nInitial address a: " << &a << " b: "<< &b;
cout << " ptr: "<< &ptr << endl;
ptr = &a; //set ptr to the address of a
b = *ptr; //b gets the value of the location pointed to by ptr
cout << "\nFinal values a: " << a << " b: "<< b;
cout << " ptr: " << ptr << endl;
cout << "\nFinal address a: " << &a << " b: "<< &b;
cout << " ptr: "<< &ptr << endl;
cout << "\nThe value of memory location " << ptr << " is " << *ptr << endl;
cout << "\na is " << a << " b is " << b << endl;
cout << "\nagain a is " << *(&a) << endl << endl;
}
/*********************Program Output**************************
Initial values a: 7 b: 0 ptr: 0x00000000
Initial address a: 0x0012ff90 b: 0x0012ff94 ptr: 0x0012ff98
Final values a: 7 b: 7 ptr: 0x0012ff90
Final address a: 0x0012ff90 b: 0x0012ff94 ptr: 0x0012ff98
The value of memory location 0x0012ff90 is 7
a is 7 b is 7
again a is 7
/**************************************************************/