Sample Program Using Pointers


#include <iostream.h>

int main()
{
   int i= 10;
   int *loc;
   int j = 100;
   int *ptr;

   loc = &i;
   ptr = &j;
   cout << "i = " << i << "   j = " << j << endl;
   cout << "i is stored in location " << loc << endl;
   cout << "j is stored in location " << ptr << endl;

   *loc = 20;
   *ptr = *loc + 1;
   cout << "i = " << i << "   j = " << j << endl;
   cout << "i is stored in location " << loc << endl;
   cout << "j is stored in location " << ptr << endl;
}

Output:

i = 10   j = 100
i is stored in location 0x51c72466
j is stored in location 0x51c72460

i = 20   j = 21
i is stored in location 0x51c72466
j is stored in location 0x51c72460


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2001