First, you should notice something new. The variables, a and b have the * character in front of them. In C++, the * character in front of a variable usually means the variable is a pointer. Pointers are one of the most difficult things about C and C++ to grasp as a new concept.
A pointer is a variable that doesn't hold a normal value, instead it holds the address of another variable, (address meaning where the other variable is in memory) .
Just so you can get the general idea look carefully at the following code..
int x = 5;
int *y = &x;
On the first line we have a normal integer variable x, set to hold the value 5.
On the second line, the variable y is declared as an integer pointer - the * in front of y means it is going to hold an address value.
It is then set equal to the &x. This does not mean that y is set to the same value as x. It is set to hold an address value, the address of x in memory. The address of x is specified by using the & operator, i.e. &x means the address of x.
This is what I get when I run the following code..
int x = 5;
int *y = &x;
cout << "The value of x is " << x << endl;
cout << "The value of y is " << y << endl;
The value 0x0012FF7C is the address of x in memory. This memory location actually holds x's value of 5.
Now, to get back to the swap example...
void swap(int *a, int *b) {
int temp = *a; // set temp to the value in the address pointed to by a
*a = *b; // set the value in address a to the value in address b
*b = temp; // set the value in address b to the value held by temp
}
.. both variables a and b are pointer variables, which mean they are going to hold addresses. The function parameters a and b are passed two addresses. The code in the body of the function proceeds to swap the values held at these addresses.
Look at the swap code in full below:-
|
#include <iostream.h>
void main() {
int x = 2, y=3;
cout << "The value of x is " << x << " and y is " << y << endl;
swap(&x, &y);
cout << "The value of x is " << x << " and y is " << y << endl;
}
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
|
Here is the output from the code:-
In this example, the variables x and y are initially assigned the values of 2 and 3 respectively. Then swap is called and the addresses of x and y are passed to a and b, (not their values).
Inside the swap function, a and b now point to x and y, so a and b can directly access the values held at x and y's addresses. These values are then swapped - x ends up with y's value of 3 and y ends up with x's value of 2.
In contrast to the swap example where the arguments are passed by value, in this case because of the call by reference, the values of x and y can be changed outside main, from directly within the swap function.
A final word. Learning about pointers and addresses and passing values by reference can be challenging and is the focus of the next C++ level 3. These aspects of C++ can be the extremely frustrating to learn but are well worth it because they bring to your programming a new level of power and are one of C++'s most important features.