I know it appears to behave differently, but if you look a little closer you'll see it acts the same. I can't remember from which book I read this, but there is a neat little mnemonic device that can help keep things clear. It goes like this:
● Read the reference operator, '&', as the "value at the address of ..."
● { int &i = var; } : "assign the integer value at the address of 'i' to the value of 'var'
● Read the dereference operator, '*', as "the address pointed to by ..."
● { int i = *pVar; } : "assign 'i' to the integer value at the address pointed to by 'pVar'
#include <iostream>
using namespace std;
int main()
{
int * PointerVar; // 'PointVar' points to the value at the address of an integer value
int Var = 10; // Assign the integer 'Var' to the value '10'
PointerVar = &Var; // Assign the value at the address pointed to by 'PointerVar' to the address of 'Var'
// the above is equivalent to:
int var = 10; // assign the integer 'var' to a value of 10
int *pVar = &var; // (helps to read this as the compiler would--right side first)
// address of 'var' is assigned to the address pointed to by 'pVar'
cout<<*PointerVar<<endl; // output the value pointed to by PointerVar
cin.get();
return 0;
}
Your second example is what is called "passing by reference." Technically, this means when the function is called, the address of the parameter is pushed to the stack rather than the value itself. The implication is that changes to referenced parameters are reflected in the caller's environment. For example:
void swap(int &a, int &b) // the value at the address of a is an integer
{
int t = a; // t equals the value of a
a = y;
b = t;
cout << "... swapped values ..." << endl;
}
void fail_swap(int a, int b)
{
int t = a;
a = y;
b = t;
cout << "... swapped values ... (fail)" << endl;
}
int main()
{
int x = 5;
int y = 2;
cout << "1) x = " << x << ", y= " << y << endl;
swap(x,y);
cout << "2) x = " << x << ", y= " << y << endl;
fail_swap(x,y);
cout << "3) x = " << x << ", y= " << y << endl;
return 0;
}
// outputs:
// 1) x = 5, y = 2
// ... swapped values ...
// 2) x = 2, y = 5
// ... swapped values ... (fail)
// 3) x = 2, y = 5
Here are some more examples of valid uses of pointers, the dereference operator, and the reference operator:
int i = 10;
int *pVal = &i; // as above
int j;
int k = 50;
j = *pVal; // now j equals 10
*pVal = k; // now i equals 50
// pVal points to the value at the address of i. Thus, when you
// assign j to the value pointed to by pVal, you are assigning j
// to the value of i. When you assign the k to the value at the
// address pointed to by pVal, you are assigning i to the value of k.
int i = 20;
int &r = i;
++r; // now i equals 21
// the address of r is assigned to the value of i. Thus, r and i
// will always have the same value. Incrementing r increments i as
// well because they have the same address in memory. If the next
// statement was:
r += i;
// both r and i equal 42. It is equivalent to:
int a = 21;
a += a;
And now, seeing as how I've come to producing the answer to the question of live, the universe, and everything ... I think it is time to stop.
Hope that helps,
~Andrew~