Pointers

A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer to a bit field or a reference.

Some common uses for pointers are:
  • To access dynamic data structures such as linked lists, trees, and queues.
  • To access elements of an array or members of a structure or C++ class.
  • To access an array of characters as a string.
  • To pass the address of a variable to a function. (In C++, you can also use a reference to do this.) By referencing a variable through its address, a function can change the contents of that variable.

Note that the placement of the type qualifiers volatile and const affects the semantics of a pointer declaration. If either of the qualifiers appears before the *, the declarator describes a pointer to a type-qualified object. If either of the qualifiers appears between the * and the identifier, the declarator describes a type-qualifed pointer.

The following table provides examples of pointer declarations.
Table 1. Pointer declarations
Declaration Description
long *pcoat; pcoat is a pointer to an object having type long
extern short * const pvolt; pvolt is a constant pointer to an object having type short
extern int volatile *pnut; pnut is a pointer to an int object having the volatile qualifier
float * volatile psoup; psoup is a volatile pointer to an object having type float
enum bird *pfowl; pfowl is a pointer to an enumeration object of type bird
char (*pvish)(void); pvish is a pointer to a function that takes no parameters and returns a char
C++11 begins nullptr_t pnull; pnull is a null pointer that does not point to any valid object or function.