Figure 2.4 Demonstration of Pointers and Addresses

//--- Demonstration of pointer variables and addresses

 

#include <iostream>

using namespace std;

 

int main()

{

  int i = 11,

      j = 22;

  double d = 3.3,

         e = 4.4;

                     // Declare pointer variables that:

  int * iPtr,        //    store addresses of ints

      * jPtr;

  double * dPtr,     //    store addresses of doubles

         * ePtr;

  iPtr = &i;         // value of iPtr is address of i

  jPtr = &j;         // value of jPtr is address of j

  dPtr = &d;         // value of dPtr is address of d

  ePtr = &e;         // value of ePtr is address of e

  cout << "&i = " << iPtr << endl

       << "&j = " << jPtr << endl

       << "&d = " << dPtr << endl

       << "&e = " << ePtr << endl;

}

 

 

Figure 2.4 Execution Trace

&i = 0012F05C

&j = 0012F060

&d = 0012F064

&e = 0012F06C

 

 

 

Figure 2.5 Demonstration of Dereferencing Pointers

 //--- Demonstration of dereferencing pointers

 

#include <iostream>

using namespace std;

 

int main()

{

  int i = 11,

      j = 22;

  double d = 3.3,

         e = 4.4;

                     // Declare pointer variables that:

  int * iPtr,        //    store addresses of ints

      * jPtr;

  double * dPtr,     //    store addresses of doubles

         * ePtr;

  iPtr = &i;         // value of iPtr is address of i

  jPtr = &j;         // value of jPtr is address of j

  dPtr = &d;         // value of dPtr is address of d

  ePtr = &e;         // value of ePtr is address of e

 

  cout << "\nAt address " << iPtr

       << ", the value " << *iPtr << " is stored.\n"

       << "\nAt address " << jPtr

       << ", the value " << *jPtr << " is stored.\n"

       << "\nAt address " << dPtr

       << ", the value " << *dPtr << " is stored.\n"

       << "\nAt address " << ePtr

       << ", the value " << *ePtr << " is stored.\n";

}

 

 

 

Figure 2.5 Execution Trace

 

At address 0012F05C, the value 11 is stored.

 

At address 0012F060, the value 22 is stored.

 

At address 0012F064, the value 3.3 is stored.

 

At address 0012F06C, the value 4.4 is stored.