1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
/*
Introduce int variables x, y, z and int* pointer variables p, q, r. Set x, y, z to three distinct values.
Set p, q, r to the addresses of x, y, z respectively.
(1) Print with labels the values of x, y, z, p, q, r, *p, *q, *r.
(2) Print the message: Swapping pointers.
(3) Execute the swap code: r = p; p = q; q = r;
(4) Print with labels the values of x, y, z, p, q, r, *p, *q, *r.
*/
#include<iostream>
using namespace std;
void main()
{
int x,y,z;
int *p,*q,*r;
x=1;
y=2;
z=3;
p=&x;
q=&y;
r=&z;
cout<<"value of x is "<<x<<endl;
cout<<"value of y is "<<y<<endl;
cout<<"value of z is "<<z<<endl;
cout<<"value of p is "<<p<<endl;
cout<<"value of q is "<<q<<endl;
cout<<"value of r is "<<r<<endl;
cout<<"value of *p is "<<*p<<endl;
cout<<"value of *q is "<<*q<<endl;
cout<<"value of *r is "<<*r<<endl;
cout<<"SWAPPING POINTERS!"<<endl;
int w;
w=*r;
r=p;
p=q;
q=r;
cout<<"value of x is "<<x<<endl;
cout<<"value of y is "<<y<<endl;
cout<<"value of z is "<<z<<endl;
cout<<"value of p is "<<p<<endl;
cout<<"value of q is "<<q<<endl;
cout<<"value of r is "<<r<<endl;
cout<<"value of *p is "<<*p<<endl;
cout<<"value of *q is "<<*q<<endl;
cout<<"value of *r is "<<*r<<endl;
}//end main
|