C++ Pointer Basics

A pointer is a special type of variable that contains an actual memory address of the value of another variable. Declaring a pointer is very similar to declaring an ordinary variable.

variable_type *variable_name;

The variable type, in the case of a pointer, is the type of data that it is pointing to, such as ‘char’, ‘string’, ‘int’, ‘float’ and ‘double’. The variable name is the name given to the pointer. The asterisk is what denotes this variable as a pointer and is sometimes referred to as an indirection operator, or dereferencing operator. It should be noted that an asterisk is also an arithmetic operator used to multiply one value by another. Below are some examples of pointer declarations.

char *pFirstInitial;
string *pFullName;
bool *pResult;
int *pAge;
float *pPrice;
double *pTotal;

A ‘p’ is often used at the start of a name for a pointer to show that it is a pointer and not an ordinary variable. It is often seen as good practice to initialise a pointer to null where it is not being given another value when it is declared. This is done by using the ‘nullptr’ keyword and is known as a null pointer. A pointer that is not initialised contains garbage data and could be pointing anywhere.

char *pFirstInitial = nullptr;
string *pFullName = nullptr;
bool *pResult = nullptr;
int *pAge = nullptr;
float *pPrice = nullptr;
double *pTotal = nullptr;

In order to assign a memory address to a pointer, which points to the value of another variable, the ‘address of’ operator needs to be used, which is an ampersand.

int age = 30;
int *pAge = nullptr;

pAge = &age;

As can be seen, when assigning the address of a variable to a pointer, the asterisk does not need to precede the pointer name. An exception to this would be if the pointer is being initialised with a value, other than NULL, when it is declared.

int age = 30;
int *pAge = &age;

The value that a pointer refers to can be used in the same way as accessing the variable using its name.

int age = 30;
int *pAge = &age;
int result = 0;

result = *pAge + 5;

cout << "Result = " << result << endl;

Here, five is added to the value that the pointer refers to and the result is stored in the variable ‘result’, which is displayed in the console.

Result = 35

Similarly, the value that the pointer refers to can be updated, as with an ordinary variable. The new value can still be accessed using the original variable name.

int age = 30;
int *pAge = &age;

*pAge += 5;

cout << "New Age = " << age << endl;

When dealing with pointers, it is possible to access both the value that it stores, which is a memory address, as well as the value that the memory address refers to.

int age = 30;
int *pAge = &age;

cout << "pAge's value: " << pAge << endl;
cout << "Value pointed to by pAge: " << *pAge << endl;

Note that the asterisk is not used to access the memory address that it stores, but it is used to access the value that the address points to. The output from the above is as follows.

pAge's value: 0060FF08
Value pointed to by pAge: 30

The memory address displayed here is a hexadecimal number.

Why use pointers?

There are a number of situations where using a pointer is preferential and even mandatory in some circumstances.

Where a C++ program is being used in an environment where resources, such as memory, are minimal, their use is particularly beneficial because they are said to be very efficient. Even where resources aren’t so scares, pointers can help a program run faster and use less memory.

There are some functions built-in to the C++ Standard Library that will only accept pointers, as opposed to an ordinary variable.

A further major advantage, in relation to functions, is that pointers allow for variables to be passed by reference to a function, meaning that more than one value can be returned from it.

It is also possible to allocate memory dynamically using pointers, depending on the use of the program.