Pointers are prevalent throughout C++ and it is important to understand them in order to take advantage of many of features that exist in C++. However, for a beginner, they can be rather tricky to grasp, especially given how confusing the syntax can be. This article attempts to alleviate the pain of learning pointers, by explaining what they are, how to use them, and why we would use them.
What is a Pointer?
A pointer is a variable that stores the memory location instead of the data itself. The basic syntax for a pointer looks like the following:
The above syntax shows a typical example of how to declare a pointer. The address-of operator ‘&’ is used to obtain the address of the data stored in the variable a, and it is that address that is stored in ptr. Although our pointer stores a memory address it is still necessary to declare the type of data that the memory address stores, so that the pointer can be used to manipulate the data.
Note: The ‘*’ operator is used to declare that the variable is a pointer, the position of ‘*’ can be positioned int* pointer or int *pointer, however the former declaration can be slightly confusing as it mimics the syntax required to dereference (explained next) a pointer.
Dereferencing a Pointer
As previously mentioned a pointer stores the memory location and not the data, therefore a pointer is pretty useless at using that data unless you can gain access to it. Dereferencing does exactly that, and provides a way for you to access the data stored in the memory location of a pointer. Dereferencing ptr looks like the following:
In this example we use the dereference operator ‘*’ (yes, it is the same operator used for declaring a pointer) to gain access to the data stored at the address it points to, in this case it is the value 12. The value is stored within a variable b and is then used in a simple expression.
Note: Care must be taken when more than one pointer holds the address to the same data. Altering the data with one pointer reference in one part of your code will affect the data being used by all the others, and this can cause undesired behaviour.
So far pointers have been described using C++ fundamental types, however pointers can be used to store the address of an object. The syntax is just a little bit trickier when accessing member data or methods. The following example shows one way of using a pointer with an object of type Student.
The second statement dereferences the pointer containing the address of the object, and then uses the dot operator to access the member function GetMathMark(). Without the parenthesis, the statement would first try to access the method of the object and then attempt to dereference, due to the operator precedence defined in C++. Given that the pointer stores an address and not the type Student this would not work out well. The syntax can be rather troublesome, therefore there is an alternate way which uses the “->” operator, which allows pointer to member access, and removing the need for parenthesis.
Pointer to a Pointer
Another aspect of pointers is that they are able to store the address of other pointers. Yes, pointers can store the address of a pointer, which the pointer of the address it stores points to the address of some data. An example is shown below using C++ fundamentals, but this also works on user-defined types.
In this case, pptr is initialised with the address of ptr not the address of the variable a, and a can be accessed by either dereferencing ptr using a single “*” or through pptr using two “*”. As you can see the syntax for a pointer to a pointer is similar to that of a normal pointer, with the exception of an additional “*” in both the pointer declaration and when dereferencing.
When a Pointer is Required
So now the basics of pointers has been explained, the next question is why do we need pointers? Pointers are used extensively in C++, and below are a few examples, with no emphasis on describing any particular use case, as each could be an article of their own.
- Dynamic allocation
- Efficient parameter passing (Although it is better practice to use references)
- Function pointers
Therefore, as you can see pointers are required if you want to make the most of C++, and make use of certain features of the language such as polymorphism, which is an important concept in any object orientated language.
To summarise, this article has introduced pointers in C++ and has provided a basic understanding of what they are, how they are used, and what they are used for.
- A pointer holds the address of a variable, not the data itself.
- The data stored at the memory address the pointer stores can be accessed by dereferencing the pointer.
- A pointer can be used to store the address of an object, and requires parenthesising the object being dereferenced before using the member access operator, or using the “->” operator
- Pointers can store the address of other pointers and is signified by the number “*” used when declaring and dereferencing a pointer