The C++Course provides a general introduction to programming in C++. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details.

Declaring Pointers and References

When declaring a pointer to an object or data type, you basically follow the same rules of declaring variables and data types that you have been using, only now, to declare a pointer of SOMETYPE, you tack on an asterix * between the data type and its variable.

SOMETYPE* sometype;

int* x;

To declare a reference, you do the exact same thing you did to declare a pointer, only this time, rather than using an asterix *, use instead an ampersand &.

SOMETYPE& sometype;

int& x;

As you probably have already learned, spacing in C++ does not matter, so the following pointer declarations are identical:

SOMETYPE*  sometype;
SOMETYPE * sometype;
SOMETYPE  *sometype;

The following reference declarations are identical as well:

SOMETYPE&  sometype;
SOMETYPE & sometype;
SOMETYPE  &sometype;

Last Update: 2005-12-05