I am little confused now. When I want to use a pointer as an argument in a function call, I have to pass the address that the pointer references to the function right?
You can pass an address or a pointer:
Code:
#include <iostream>
using namespace std;
void swap (int *x, int*y);
int main()
{
int i=10;
int j=20;
cout <<"Now first integer is:"<<i<<endl;
cout <<"Now second integer is:"<<j<<endl;
swap(&x,&y);
int* pi = &i;
int* pj = &j;
swap(pi, pj);
cout <<"Now first integer is:"<<i<<endl;
cout <<"Now second integer is:"<<j<<endl;
return 0;
}
void swap (int *x, int *y)
{
int temp=*x;
*x=*y;
*y=temp;
}
You can pass a 'literal' address(&i, &j) or a variable that stores an address(pi, pj).
I know what passing by reference means
There really is no such thing--everything is passed by value. Run this program and carefully examine the addresses that are displayed:
Code:
#include <iostream>
using namespace std;
void func(int* p)
{
int i = 100;
p = &i;
cout<<p<<endl; //display new address
}
int main()
{
int n = 5;
int* pn = &n;
cout<<pn<<endl; //display address
func(pn);
cout<<pn<<endl; //did anything change?
return 0;
}
The first and the second addresses that are displayed should be indentical, which proves a pointer is passed by value--the function cannot permanently change the address stored in the pointer.
However, you can use a copy of a pointer to permanently change the object stored at that address. It doesn't matter whether you have the original address or a copy of the address--either one can be used to make permanent changes to the object stored at that address.
...but I am not sure passing a reference means.
A reference is implemented as a constant pointer. A constant pointer is a pointer that always has to point at the same address. The value at that address can change, but the pointer cannot be assigned another address.
Passsing a reference means you are passing a constant pointer to the function, and just like with a regular pointer, a copy is made for the function. References just have a different syntax than pointers. Here is an example:
Code:
#include <iostream>
using namespace std;
void func(int& r)
{
int j = 400;
r = j;
cout<<r<<endl;
}
int main()
{
int n = 5;
int& r = n;
cout<<r<<endl;
func(r);
func(n); //Can send a normal variable or a reference variable to the function
cout<<"after function call: " <<r<<endl;
return 0;
}
Inside the function, this line:
Code:
void func(int& r)
{
int j = 400;
r = j;
cout<<r<<endl;
}
is the crucial line. It looks like the reference variable is being made to refer to another variable(i.e. the address is being changed which is not allowed for a constant pointer). However, the syntax of a reference variable hides what is really happening. What is really happening is that a copy of the reference/constant pointer was made for the function, and the assignment statement is using the address of the pointer to change the value at that address--just like when you pass a regular pointer to a function. As an experiment, remove the & in the function parameter, and then run the program again and reexamine the output.