Swapping two variables in C++
In this post, we consider about how to swap two variables in C++. Assumption that we would like to swap two integer variables.
Let’s look at the first swap1 function below.
Seem ok, but it does not work. Because a and b are local variables in the swap1 function. When you call swap1(x, y), actually x will be copied to the a parameter, y also. Inside the swap1 function, a and b are exchanged but not given x and y variables. a and b are discharged when finished swap1 function.
Moving to the second swap2 funtion below where reference variables are used as parameters.
It works well because of reference variables. When you call swap2(x, y) function. Actually, we will have int &a = x, int &b = y. Now a and b are reference variables. So a and b are attached to the x and y location in memory. You can access variables through either a, b or x, y. When you change a and b value, in essence you are changing x and y. References is good but it has some disavantagges:
- you cannot have NULL references. You must indicate that a reference is connected to a piece of memory.
- Once a reference is initialized to an object, it cannot change to refer to another object.
- A reference must be initialized when it is created.
From that points, using pointers in C++ may be flexiable and powerful. Parameters of swap3 function are two pointer variables of integer. When you call swap3(&x, &y), we have int *a = &x, int *b = &y. Pointer variables a, b will point to x, y, respectively. Actually, a and b store the address of x and y in memory. We can access the value pointed by a by operator *a.
Enjoy Reading This Article?
Here are some more articles you might like to read next: