Learning C++: The Basics Of Pointers

An example "Hello World" in C++

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;
Example C++ pointer
Example C++ pointer

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.

Showing that the memory address changes on each run of the program
Showing that the memory address changes on each run of the program

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

Changing the number stored within value without accessing value.
Changing the number stored within value without accessing value.
#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.

Although the number stored in value is changed from 2 to 4, it was not done via the variable value.
Although the number stored in value is changed from 2 to 4, it was not done via the variable value.

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:

secondValue is reading from *newPointer
secondValue is reading from *newPointer

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.

value and secondValue may both contain the number 4, but they are stored in different memory addresses
value and secondValue may both contain the number 4, but they are stored in different memory addresses

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.