Swapping addresses

I am trying to swap the address of two variables from the main function. But address is only swapped inside the function and it does not affect the same variables in the main function. How do I swap the original variables' addresses?

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

#include<iostream>

using namespace std;

void swapAddress(int *&x, int *&y)//*&x is a reference to an integer pointer.
{
   cout << "@@Before Address(x) :" << x << endl;


   int *temp = x; 
   x = y;   

   cout << "@@After Address(x) :" << x << endl;

   y = temp; 

   //The address of x and y are swapped in this function but is not affected outside the scope of this function.

}



int main ()
{

   int a = 1;
   int b = 2;

   int *apointer= &a, *bpointer=&b;

   cout << "Before Address(a) :" << &a << endl;
   cout << "Before Address(b) :" << &b << endl;

   swapAddress(apointer, bpointer);

   cout << "After Address(a) :" << &a << endl;
   cout << "After Address(b) :" << &b << endl;

   return 0;
}
Last edited on
You cannot move a variable in memory. Once a variable is created it stays where it is for its entire lifespan.

The closest you can do to what you are attempting is to copy the variable to a different variable (ie, swap the contents of a and b, rather than their addresses)
closed account (48T7M4Gy)
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
#include<iostream>

using namespace std;

void swapAddress(long& x, long& y)
{
    long temp = x;
    x = y;
    y = temp;
}

int main ()
{

   int a = 1;
   int b = 2;

   int* apointer = &a;
   int* bpointer = &b;

   cout << "Before Address(a) :" << apointer << endl;
   cout << "Before Address(b) :" << bpointer << endl;

   swapAddress( (long&)apointer, (long&)bpointer );

   cout << "After Address(a) :" << apointer << endl;
   cout << "After Address(b) :" << bpointer << endl;

 cout << " a:" << *apointer<< endl;
   cout << " b:" << *bpointer<< endl;

   return 0;
}
Last edited on
You can swap pointers because pointers are normal variables.

This does not do what I think OP is trying to do... which is to actually physically swap a and b in memory. That is not possible.

For an example of what OPs code is doing.... and why it isn't working... let's plug in some numbers:

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
   int a = 1;   // let's say 'a' is put at address 0
   int b = 2;   // and 'b' is put at address 4
   // THEY WILL BE AT THIS ADDRESS FOREVER.  You cannot change that.
   
   // memory at this point:
   //   0    4    8    C     <-  address
   // +====+====+====+====+
   // | 1  | 2  | ?  | ?  |  <-  contents
   // +====+====+====+====+
   //   ^    ^
   //   |    |
   //   a    b

   // 'apointer' is put at address 8...  contains the address of 'a'
   // and 'bpointer' is at address C... and contains the address of 'b'
   int *apointer= &a, *bpointer=&b;
   
   // memory at this point:
   //   0    4    8    C     <-  address
   // +====+====+====+====+
   // | 1  | 2  | 0  | 4  |  <-  contents
   // +====+====+====+====+
   //   ^    ^    ^    ^
   //   |    |    |    bpointer
   //   a    b    apointer

   cout << "Before Address(a) :" << &a << endl;  // <- these will print the address of 'a', as expected
   cout << "Before Address(b) :" << &b << endl;  // <- and the address of 'b', as expected
   
   swapAddress(apointer, bpointer);  // <- this will swap the contents of apointer and bpointer
     // note this does not move any variables... it merely changes the contents of the variables:
     
   // memory at this point:
   //   0    4    8    C     <-  address
   // +====+====+====+====+
   // | 1  | 2  | 4  | 0  |  <-  contents
   // +====+====+====+====+
   //   ^    ^    ^    ^
   //   |    |    |    bpointer
   //   a    b    apointer
   //                |     |
   //                 \   /
   //                  \ /
   //                swapped 


Note that all this accomplished was it made 'apointer' point to b, and made 'bpointer' point to a. It did not change 'a' or 'b'.
closed account (48T7M4Gy)
Really Disch? Wow!
Topic archived. No new replies allowed.