My learning adventures in C++ are going quite well and I think I have made notes (via my previous blog posts) on most of the C++ basics, which means it is time to discuss pointers. As I have wrote in my previous blog entries, I’m still finding my feet with C++ so apologies if this is a little long winded.
Pointers
When a variable is created in C++, C++ stores the variables value within memory using a memory address. A pointer refers to, or points to, the memory address.
To define a pointer an * is used. Yep, the same * that is used to do multiplication is used to also define a pointer and also to dereference it later on.
int* myPointer;
The variable type should be used followed by the *. In the above example I have created a pointer that will reference an integer variable.
To point the pointer at a variables memory address the & is used. The & is the “address of operator” and is used in front of the variable that is having its address mapped.
*myPointer = &variableName;
It is recommended not to leave a pointer without an address mapped, and the pointer can be created and mapped on one line instead of two:
int* myPointer = &variableName;

In this example I have created a variable called “value” and stored the value 2 within it. I have then created a pointer called “newPointer” and pointed it to values memory address using &value.
I am then outputting the values.
#include <iostream> using namespace std; int main() { int value = 2; int* newPointer = &value; cout <<"value =" <<endl; cout << value << endl; cout << " " << endl; cout << "NewPointer =" << endl; cout << newPointer << endl; return 0; }
Although the variable value is always “2”, the memory address changes each time the program runs as it is stored in the computers memory. For example, on the first run the value was stored in 0xbeae9578, on the second run it was stored in 0xbec4d578, then on the third run it was 0xbeb1d578.

The pointer can be used to directly change the integer held in memory for value, rather than calling value to change it.

#include <iostream> using namespace std; int main() { int value = 2; int* newPointer = &value; cout <<"value =" <<endl; cout << value << endl; cout << " " << endl; cout << "newPointer =" << endl; cout << newPointer << endl; cout << " " << endl; cout << "*newPointer = " << endl; cout << *newPointer << endl; cout << " " << endl; *newPointer = 4; cout << "value =" << endl; cout << value << endl; cout << " " << endl; cout << "newPointer = " << endl; cout << newPointer << endl; cout << " " << endl; cout << "*newPointer =" << endl; cout << *newPointer << endl; return 0; }
When the program runs the number in value is changed from 2 to four and the memory address stays the same.

Dereferencing A Pointer
A key point to pointers is the * and rule:
If the * is on the left of the =, then the pointer is being written to.
If the * is on the right of the +, then the pointer is being read from.
For example:

This is the same as my original code except for the introduction of the secondValue which reads from the *newPointer (notice it is on the right of the =). As it has read the value from *newPointer, the number in secondValue has its own memory address separate from variable called value and newPointer.
int secondValue = *newPointer; cout << " " << endl; cout << "secondValue = " << endl; cout << secondValue << endl; int* secondPointer = &secondValue; cout << " " <<endl; cout << "secondPointer =" << endl; cout << secondPointer << endl;
value and newPointer both use the number 4 in memory address 0xbe80d574, and although secondValue read the number 4 from newPointer it stores its number 4 in memory address 0xbe80d570.

You must be logged in to post a comment.