Analytics Vidhya
Published in

Analytics Vidhya

Pointers Made Simple (Finally…)

A notorious, nessecairy and neat subject for programmers

Photo by Andrey Metelev on Unsplash

Introduction

Besides student loans and basic social contact nothing scares programmers as much as the concept of pointers. Especially the introduction of pointers (frequently in college) can be very intimidating for beginners. I personally remember very well how much effort it costed me to finally come to that “ah! Now I get it!” moment in my first year of college. That’s why this article will help you skip all those panic nights behind your laptop and directly get a better understanding of this subject.

To achieve this, this article will first explain pointers on a concept level through an example accompanied with illustrations. Thereafter code will be added (in C++) to solve the example using pointers. This will not only showcase how pointers work but also why they are so important.

Pointers at a concept level

Imagine a post office with four different postboxes. Every postbox has an address, a name (which belongs to the owner) and some delivered letters. Currently these four postboxes belong to John, Eve, Raj and Adah.

If someone wants to send a letter to Eve that person must know her address and put that letter in her postbox. Eve can then access that postbox later and retrieve the letter. Simple enough right?

Well that’s essentially the same concept as pointers! Only with pointers the addresses are often represented as hexadecimal numbers whilst the postboxes are objects, which makes the whole thing seem a lot more intimidating then it actually is. Therefore, in this article we’ll use this example to make pointers less abstract and more understandable.

Pointer translated to a code level

If we want to code the example shown above we first need to make the postboxes. In C++ this means that we need to make an object (can either be a class or a struct) with all the necessary attributes. To reiterate, every postbox has an owner, an address and a collection of delivered letters.

struct postbox
{
std::string owner = "";
std::vector<std::string> letters;
};

This code snippet creates that postbox. The owner’s name is saved as a string and the collection of letters is saved in a vector. For those who are not familiar with vectors; they are essentially arrays with a variable length. This comes in handy because we don’t know how many letters each postbox has.

However, the postbox described earlier had three attributes whilst this postbox only has two. This is because the address is not a variable we add to the postbox but instead a variable we point to! This is where pointers become useful.

Pointers are essentially variables that contain a memory address. The declaration of a pointer in C++ requires a * whilst a memory address is denoted using an &. This syntax might make the whole thing seem very vague but the concept (as illustrated earlier) doesn’t really change.

struct postbox *eve_address, eve;
eve_address = &eve;

This code snippet creates a postbox with an address. The name of the postbox is eve and the name of the pointer to her postbox’s address is eve_address. We can direct the pointer of eve_address to her postbox by giving it the value of the postbox’s memory address, because as mentioned, a pointer is nothing more then a variable that contains a memory address.

This memory address can be accessed through using an &. This is what we do in the second line of the code snippet. By giving the pointer the value of eve’s address we now have a direct line to her postbox! To get an idea of what this looks like try outputting eve_address through the standard output, that’s a lot of hexadecimal jargon!

Applying pointers in practice

But how can we use this (memory) address? And what does it actually mean? Well to get back to the postbox metaphor, an address is the location where content is stored. This definition also applies for computer memory addresses.

Therefore the application of this address is that we can use it to access the attributes from the postbox. In other words, if we know the address, we can send a letter to it. Just like the real world!

eve_address->owner = "eve";
eve_address->letters.push_back("hey eve! how you doin?");

To do this use the -> operator. This operator, some would call it an arrow, gives access to the attributes stored on a specific memory address. So if we want to change the name of the owner we use ->owner = and if we want to add a letter to the vector we use ->letters.push_back().

Now that the letter is sent to the right address Eve can access it through the postbox the pointer points to. After all, the address is the same regardless of if you’re a sender or a recipient. The first letter of the vector is letter number zero, so to access it use std::cout << eve.letters[0];. Seems as if Eve is making a lot of friends…

Final words

Pointers are a notorious subject. What this article hopefully showed is that even with all the syntactic jargon the concept is quite simple and intuitive. What’s important to remember is that even if the application of pointers get more complicated the principles shown in this article still apply. So if you’re ever stuck using pointers and it seems as if there’s no way out? Draw your code out and use simple metaphors, it really helps.

If you’d like to continue to experiment with the code and learn more about it feel free to do so. The complete source code used in this article is given in the gist below. Thank you for reading my article!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store