7.1.2 Assigning and Updating Pointers

As mentioned before, pointers are a variable type that holds a memory location that points to a value. To use pointers, you need to have a variable created as the pointer in the stack and an actual value created in the heap.

Pointer Syntax

Before getting into the details of how pointers work, let’s take a look at the syntax.

To create a pointer variable, you still need to tell C++ what type of variable the pointer will point to. An integer pointer can only point to an integer. A string pointer can only point to a string, etc.

With this, the syntax is very similar to the standard variable declaration. To declare a variable as a pointer, you simply add a * before the variable name.

Example:

int *num; // Integer pointer
string *name; // String pointer
double *rate; // Double pointer

Once a pointer is created, a memory address can be assigned to that pointer. Many times you will get that memory address for a preexisting value, such as a variable’s reference value or another pointer variable.

If you do need to assign it to a new memory location, you use the new keyword to create a new value.

Example:

int *num;
num = new int;

You can then assign the value to that pointer:

*num = 10;

The use of the * here actually has a different meaning. The * here actually is the dereference operator. It actually says to take the value that the pointer is referencing and update that to 10.

Remember above how we said that pointers can be prone to errors? You will see more uses of the *, but keep in mind that you use the star to initially declare a pointer type, but after that you will use the star when you want to refer to the value stored at the pointer address.

You can combine some of the steps above:

int *x = new int;
*x = 10;

double *y = new double(4.5);

In the example above, both x and y will be created as pointers with values assigned.

Access Pointer Values

Take a look at the following two print statements:

cout << num  << endl;
cout << *num << endl;

The first print statement asks to print the value of num. Remember the num as a variable is the memory address that the pointer points to. This will print something like 0x55c38056ce70.

The second statement uses the dereference operator, so it says to print the value that the reference is pointing to. From example above, this would print 10.

Stack vs Heap

To better understand how pointers work, you need to pull back the curtains to look at what happens behind the scenes. When creating and storing values, C++ has different places that it holds values, the stack and the heap. The stack tends to be used for many variables as it provides quick access (as it is usually stored in the CPU), but less flexible. On the other hand, the heap allows data to be more flexible and store larger volumes of data, but may be less efficient to access.

When you create a standard variable such as an integer, C++ creates the variable and stores the value in the stack. See the example below.

Standard Variable

In contrast, when declaring a variable as a pointer, C++ creates the variable as a pointer type that doesn’t point to anything just yet.

As you saw above, to assign the pointer to a value you need to first create a location in the heap using the new keyword. This creates that spot in the heap and sets the pointer to point to this memory location.

Pointer Variable - Step 2

Finally, you saw how you can use the dereference operator to assign a value to the heap location.

Updating Pointers

To close out this example, take a look at what happens when you have multiple pointers pointing to the same place in the heap. Notice in this example, both num1 and num2 point to the same location storing the number 10.

Once the heap value is updated via num2, num1 will still get updated since it is pointing to the same place in the heap.

Last updated