Pointers in a nutshell
A pointer is a variable whose value is an address.
They often look like this
0x602010 (some examples
taken from a program I just ran). These addresses are expressed
as numbers in hexadecimal, which is base 16 (that's
0x at the beginning means). You don't
have to know what the numbers are, but you do need to know that:
- The computer knows the address of every variable in your
- Given a memory address, the computer can find out what
value is stored at that location.
- While addresses are just numbers, C++ treats them as a
separate type. This allows the compiler to catch cases where
you accidentally assign a pointer to a numeric variable and
vice versa (which is almost always an error).
Pointers in C++
C and C++ allow you to manipulate pointers explicitly.
& operator, called the address
of operator, can be applied to a variable to find out its
&x is the address of the
&a[i] is the address of the variable
that is the element of array
&fred.age is the address of
age field in the
stored in the variable
Try out this worksheet to get some hands-on practice if you're new to pointers or need a refresh. If you don't know the answers to the questions in the worksheet, code them up!
Last updated January 09, 2018 15:07:56 EST