Swapping pointers or Swapping Addresses?
Hello Everyone!
I'm a bit lost, what's the difference between this sample I've just came across
1 2 3 4 5 6 7 8 9
|
int a = 10;
int b = 99;
int * pa = &a;
int * pb;
pb = &b;
int t = *pa;
cout << "t = " << t << endl;
*pa = *pb;
*pb = t;
|
and this sample (what I usually do):
1 2 3
|
int *pt = pa;
pa = pb;
pb = pt;
|
Thanks!
Well, one has 9 lines of code, and yours has 3.
Also, this
1 2 3
|
int *pt = pa;
pa = pb;
pb = pt;
|
Will swap the addresses pa and pb point to respectively, and this:
1 2 3
|
int t = *pa;
*pa = *pb;
*pb = t;
|
Swaps the values of the memory regions pointed to by the pointers.
Topic archived. No new replies allowed.