Pointers:
Pointers are variables
that store addresses of a certain data type.
Pointers are declared
just like a regular variable, except with a *
between the type and name:
ex. int *pNumber
= 0;
type name of pointer initializer value
To use pointers:
Use an &
in front of a regular variable name to get the address of that variable
to put into a pointer.
Use a *
in front of a pointer name to get the value stored the address pointed
to by that pointer.
References:
References are just another
name for a variable (an alias).
References are declared
just like a regular variable, except with an &
between the type and name:
ex. int &aNumber
= number;
type name of reference
the variable that the reference is another name for
References
can be treated like any other regular variable. However, any changes
to it affects the original variable.
ex. int number =
10;
cout << number; // will
produce a 10
int &aNumber = number;
number = 20;
// this also affects aNumber
cout << aNumber; // will produce
a 20
aNumber = 2;
// the same as number = 2
cout << number; // will
produce a 2
Note:
The *
and &
are referred to twice in the above decriptions.
For both, there are two
different contexts where they are used (like overloaded functions):
in
declarations of variable to create pointers and references
and elsewhere as unary operators in front of variables used to work
with addresses and pointers
Pointers vs. References:
With a reference, you have one box with the information inside
it and stamped with two names.
With a pointer, you have a box with the information
and a separate box with the location of the first box inside
and the pointer name stamped on it.
Function parameters and the different types of variables:
When a function parameter
is a normal variable (pass by value):
A copy of the argument to the function is made and given to the
function
Any changes made to that parameter in the function will not affect the
original variable
When a function parameter
is a pointer:
The address that is given as an argument will be stuck into the
pointer
The contents of the address pointed to by the pointer can be changed
to affect the original variable
When a function parameter
is a reference (pass by reference):
The function will get a variable that is another name for the argument
Whenever something is done to the reference, the value of the original
variable will also change
Example:
#include <iostream.h>
void swap1(int,
int);
// normal function parameters
void swap2(int
*, int *); //pointer
function parameters
void swap3(int
&, int &);
// reference function parameters
void main()
{
int x = 10;
int y = 20;
swap1(x,
y);
// swap the values of the copies of x and y
cout << "x= "<<x<<",
y= " << y << endl; //
will produce "x= 10, y= 20"
swap2(&x,
&y);
// swap the contents of x and y through their addresses
cout << "x= "<<x<<",
y= " << y << endl; // will
produce "x= 20, y= 10"
swap3(x,
y);
// swap the contents of x and y by using a reference
cout << "x= "<<x<<",
y= " << y << endl; // will
produce "x= 10, y= 20"
}
void swap1(int a, int b)
// Pass by value:
{
// Copy the original values from the caller,
int temp;
// but any changes don't affect the originals.
temp = a;
// ex. The original and a are located in
different places.
a = b;
b = temp;
}
void swap2(int *a, int *b)
// Passing using pointers:
{
int temp;
temp =
*a;
// Because a and b are addresses,
*a
= *b;
// this will change the original variable's
contents.
*b
= temp;
}
void swap3(int &a, int &b)
// Pass by reference:
{
int temp;
temp = a;
// Because a and b are aliases for the
original variables,
a = b;
// this will change the original variable's
contents.
b = temp;
}