How does a swap function work with references?

I want to swap two variables with a function. I create two functions my_swap_f1 and my_swap_f2; my_swap_f2 works perfectly but my_swap_f1 throws 2 errors (commented out below).

#include using namespace std; int my_swap_f1(int &a,int &b) < int *temp; temp=&a; //&a=&b; //throw error //&b=temp; //throw error >int my_swap_f2(int *a,int *b) < //works perfectly int temp; temp=*a; *a=*b; *b=temp; >int main()< int a=10; int b=20; int temp=0; cout

Question: Why are errors thrown in my_swap_f1 and what should I do if I want to swap with my_swap_f1 ?

6,232 4 4 gold badges 40 40 silver badges 56 56 bronze badges asked Oct 24, 2017 at 19:34 H. M. MOHIDUL ISLAM SHOVON H. M. MOHIDUL ISLAM SHOVON 99 3 3 silver badges 8 8 bronze badges using namespace std; is a bad practice, never use it. Commented Oct 24, 2017 at 19:35 can u explain why its a bad practice?? @tambre Commented Oct 24, 2017 at 19:36

&a=&b; -> a=b; and &b=temp; -> b=*temp; It looks like you do not understand how to use reference variables in C++, you should review that. (Also there is no need for int* temp either)

Commented Oct 24, 2017 at 19:37 Try clicking on the link, to learn why it's bad practice. Commented Oct 24, 2017 at 19:37

2 Answers 2

One of the main reasons to implement swap with references instead of pointers is to avoid all that * and & in the code:

int my_swap_f1(int &a,int &b) < int temp; temp = a; a = b; // a/b refer to the parameters that were passed b = temp; // modifying a reference is the same as modifiying the original >

cannot work, because &a (the address of a ) is a rvalue. A rvalue is, sloppy speaking, something that you cannot assign to, it can only appear on the right side of an assignment. If it worked it would mean something like: "Take the address of a and set it to the address of b ", but of course you cannot change the address of an object like that.

answered Oct 24, 2017 at 20:13 463035818_is_not_an_ai 463035818_is_not_an_ai 121k 11 11 gold badges 97 97 silver badges 197 197 bronze badges "assing" -> "assign" Commented Oct 24, 2017 at 21:34 @MillieSmith thx fixed. btw typos like that you can also fix by yourself. Or cant you edit answers? Commented Oct 25, 2017 at 6:49

I just feel like I'm stepping on people's toes when I make a one-character edit for spelling. I guess I would have done it for someone with less rep.

Commented Oct 25, 2017 at 15:09

@MillieSmith would be completely fine with me. And imho rep is overrated, I could have made all my rep on java and still my c++ answers would look like I know what I am talking about (i didnt and still sometimes I dont know what I am talking about ;). Btw you will get a notification if someone edits your posts so you always have to chance to roll it back

Commented Oct 25, 2017 at 15:41 True. I'll edit them from now on and see if I get pushback. Thanks. Commented Oct 25, 2017 at 17:27

The difference between my_swap_f1 and my_swap_f2 is that my_swap_f2 declares its arguments to be pointer types, while my_swap_f1 declares its arguments to be reference types. References work differently from pointers in C++, and you are attempting to use references incorrectly in my_swap_f1 .

In C++ a reference type is an immutable pointer-like handle, which points to only one instance of the referred-to type and can never be reassigned. It is treated like a value of the referenced type in most expressions, meaning that you can access the referred-to instance "directly" without dereferencing anything. Thus, if you declare a reference variable int &a , the reference a points to exactly one int , and you can access that int 's value by just writing a , such as in the assignment int temp = a (note that temp is just an int , not a reference). There's no need to write &a , and doing so will in fact take the address of the int that a refers to (giving you a pointer-to- int ), because the "address of" operator ( & ) will be applied directly to the int value.

This will make more sense if you write your parameter declarations with the "pointer modifier" next to the type name rather than the variable name:

//This function's parameters are two pointers int my_swap_f2(int* a, int* b); //This function's parameters are two references int my_swap_f1(int& a, int& b); 

Then, when implementing my_swap_f1 , you can change the int value that a refers to by assigning to a directly, because a is a "reference to int ." The correct version of your function would be:

void my_swap_f1(int& a, int& b)

Note that in line 3, the assignment overwrites the int that a refers to with the value of the int that b refers to. The references themselves cannot be changed, so there is no need to add any extra symbols (like & ) to indicate the referred-to value rather than the reference itself. (Also, I changed the function return type to void , since your code does not actually return a value from the function, and it doesn't need to).