4 minute read

Introduction

Pointers can seem quite complicated, but once the basic concepts behind them are understood, they appear to be much easier to understand. Let’s have a look!

Memory addresses

Programming languages need to store variables and data in the memory space so that we can access them later.

Each object or variable (like a float, int or any other data type) is stored in a certain memory address. Therefore, each time that we declare a new object or variable in our code, we are actually allocating the variable to our memory.

We can discover where our objects or variables are stored in the memory space by using the & operator (Ampersand) in front of our variable.

Example

For instance:

#include <iostream>

int main()
{
    
    int myNumber; //Stored in a memory address
    myNumber = 5;

    std::cout << "Memory address:" << &myNumber; //The & operator shows the memory address where myNumber is stored

    return 0;
}

If we run our code, we get:

Memory address: 0x7ffffb53a0dc

Each variable has got its own memory address.

Pointers

A pointer is a variable that stores the memory address of a different variable, it points to these memory addresses and provides us with an alternative way of accessing the memory space where the referenced object is stored.

Declaring pointers

Pointers are declared by using the * operator in front of the variable like: int *pointer

Note that pointer is not pointing at any variable in the example below, by default it will point at 0. For instance:

#include <iostream>

int main()
{
    
    int* pointer; //Pointer which points at 0 by default

    return 0;
}

Referencing pointers to a variable

Next, we can make our pointer reference (or point at) a variable by changing the pointer’s value to the memory address of the object that we want to point at.

This can be accomplished by running: pointer = &myNumber; For instance:

#include <iostream>

int main()
{
    
    int* pointer; //Pointer which points at 0 by default

    int myNumber = 5;

    pointer = &myNumber; //pointer equals to the address (ampersand) of myNumber

    std::cout << "It is pointing at: " << pointer << std::endl; //Shows the value of our pointer, which is the address of myNumber
    std::cout << "Memory address of myNumber: " << &myNumber; //The & operator shows the memory address where myNumber is stored
    return 0;
}

If we run our code, we get:

It is pointing at: 0x7ffffb53a0dc
Memory address of myNumber: 0x7ffffb53a0dc

As we can see, the value of our pointer and the memory address of myNumber are indeed the same.

Using pointers

Now that our pointer references a variable, we can access and modify the variable that it is pointing to by using the * operator like: *pointer

For instance:

#include <iostream>

int main()
{
    
    int* pointer; //Pointer which points at 0 by default

    int myNumber = 5;

    pointer = &myNumber; //pointer equals to the address (ampersand) of myNumber

    std::cout << "It is pointing at: " << pointer << std::endl; //Shows the value of our pointer, which is the address of myNumber
    std::cout << "Memory address of myNumber: " << &myNumber << std::endl; //The & operator shows the memory address where myNumber is stored

    *pointer = 99; //We modify myNumber which is being referenced by the pointer

    std::cout << "myNumber is: " << myNumber << std::endl;



    return 0;
}

The code snippet above prints the following to the console:

It is pointing at: 0x7ffffb53a0dc
Memory address of myNumber: 0x7ffffb53a0dc
myNumber is: 99

Finally, it is important to keep in mind that pointers are stored in the memory space too. Therefore, each pointer has its own memory address. For instance:

#include <iostream>

int main()
{
    
    int* pointer; //Pointer which points at 0 by default
    std::cout << "Memory address of pointer: " << &pointer << std::endl; //The & operator shows the memory address where pointer is stored

    return 0;
}

Conclusion

  • A pointer is variable that stores the memory address of a different variable
  • We can declare a pointer by typing: int* pointer;
  • We can reference a pointer to a variable like: pointer = &myVariable;
  • We can access or modify the referenced variable by typing: *pointer = 41;

    References

    Read more about pointers here: https://www.cplusplus.com/doc/tutorial/pointers/