Overview
POINTERS
In the functions used so far only one variable could be returned to the calling routine. One of the ways this restriction can be overcome is by the use of pointers.
A pointer is a variable that holds the address of a variable object of a particular type. It must be declared before it is used and once initialised cannot be changed. It can only point to the type of data it has been initialised to:
int *ptr1; // pointer to int variable called ptr1
float *ptr2; // pointer to a float variable called ptr2
SYNTAX QUIBBLES
Some programmers argue that the above syntax is misleading, because the name of the pointer is ptr2 and not *ptr2. Therefore when looking at program listings, expect to see any of the following:
float *ptr2;
float* ptr2;
float * ptr2;
The compiler does not mind which one is used. The use of the second one certainly makes program listings easier to understand. All software houses have a house-style, with syntax rules that the programmers must follow.
At the moment, the pointers have not been initialised, so consequently contain garbage. We initialise them like this :
int val1;
float val2;
ptr1 = &val1; // note : no * is used here
ptr2 = &val2;
The & operator means "the address of". The pointer ptr1 now contains the address of val1 and the pointer ptr2 contains the address of val2.
cout
<< *ptr1; // would print the data at the address pointed to
// by ptr1
cout << ptr1; // would print the address ptr1 is pointing to
*ptr1 = 10; // would put the value 10 at the address ptr1 is
// pointing to
Note : when the * operator is used in the context *ptr1 it is called the "indirection operator". It is also known as dereferencing the pointer.
EXERCISE 11
Write a program that, by the use of a pointer, changes the value of a simple variable.
REFERENCES
An operator that was not implemented with C is called the reference operator. Passing a reference (also called alias), to a function, allows the function to change the value of the original variable. The use of references in C++ provides a more convenient syntax for accessing data than is possible through the use of pointers.
The reference operator & is used to pass the address of a data item.
Incorrect header, incorrect return type for main() and no namespace - not bad for page 28....
While not incorrect as such, it is a tad wasteful to have the empty prototype instead of just having the swap function defined above main()
#include <iostream.h>
void swap (int &x, int &y); // prototype
/* void swap (int& x, int& y); Some people think this is better
void swap (int&,int&); Sometimes used*/
void main()
{
int x = 10, y = 20;
cout << x << " " << y << "\n"
swap(x,y);
cout << x << " " << y << "\n"
}
void swap (int &x, int &y)
{
int z;
z = x;
x = y;
y = z;
}
Shown below is the same program, but using pointers:
Same code for main(), so why not just have the alternate version of swap() and the calling of the function?
#include <iostream.h>
void swap (int *x, int *y);
void main()
{
int x = 10, y = 20;
cout << x << " " << y << "\n"
swap(&x, &y);
cout << x << " " << y << "\n"
}
void swap(int *x, int *y)
{
int z;
z = *x;
*x = *y;
*y = z;
}
Notice the difference in function calls:
swap(x,y);
swap(&x,&y);
Also notice how much easier it is to both read and write the code using references.
Do not confuse the & "reference operator" with the & "address of" operator. This is one reason why some programmers write:
void swap (int& x, int& y); // int& x means reference to an int
// call x
instead of
void swap(int &x, int &y); // correct, but unclear
Again, this depends on house style.
EXERCISE 12:
Write a program with a function that uses a reference to pass the address of a data item. Change the value within the function and prove that it has been changed by printing it to the screen.
Large data structures and large arrays are usually passed by reference for reasons of efficiency. Each element on the array can be accessed. This is obviously a lot more efficient that passing each element of the array separately.
A classic one here - a pointer variable has to be declared before it can be used. Well, what a suprise!