So far, this book has examined several types of variables: integers, characters, floating-point numbers plus arrays and structs composed of these simple types. In previous chapters, you have been introduced to pointers and references. In this chapter, we will look at these variables in greater detail.
A pointer is a variable that points to another variable. Pointers have a type; that is, a pointer to
int points to or refers to an
int. A pointer to
char refers to a
char. A pointer to
int can be assigned to another pointer to
int, but not to a pointer to
char. A pointer to class
foo refers to an instance of class
foo. A pointer can also be the special value
nullptr, which means the pointer is not pointing to anything. A reference, which will be discussed in more detail later in this chapter, is a pointer, but with constraints that make it safer to use.
C++ pointers can point to any variable inside any data structure, and can iterate through arrays. To make pointers...