Understanding and breaking down pointers in C

One of the toughest concepts to understand is pointers in C++. For myself and others, the reason why pointers are hard to understand is because of the different operations involved with them. It doesn’t matter whether you’re a beginner or intermediate, they still confuse many programmers.

What are pointers?

A pointer is an object that contains a memory address, which is often linked to the location of another object as a variable. There are two types of operators which pointers use which are * and &. To understand the concept of pointers, we first have to understand how variables in a program are stored. We know that variables are stored in the random access memory (RAM), in which the RAM consists of memory cells, a byte long in size that contains a unique address.

Using an analogy to break down and understand pointers, we are going to create an analogy of the RAM containing several cars parked on a very long street, where each car is a memory cell. Now, there must be a way for us to find the car we want. Well, each car has a name so this would be our variable name.

int car_honda;

Currently, car_honda does not have a value stored so continuing on, we will give it a value.

car_honda = 50;

This will now store the value of 50 to the variable car_honda. Remember that the variable car_honda is now a unique number stored in the memory. If car_honda was number 500 in the memory, we would know that the car is between cars 499 and 501 when we are searching for it.

The & operator of pointers

The “&” symbol is known as the address of an operator. This operator would return the memory address of its operand. Following on from the parked cars analogy, the & operator would allow us to find where the car is located in the long street.

car_ford = &car_honda;

This line would store the address of the car_honda variable into the variable car_ford. Suppose the address of car_honda is 500.

car_honda = 50;

car_toyota = car_honda;

car_ford = &car_honda;

So in the first line will store the value of 50 in car_ford. In the second line will store the value of 50 inside car_toyota and but in the third line, it will store the address of the variable car_honda, 500, into the car_ford variable.

pointers diagram 1

The * operator of pointers

The “*” symbol is known as the dereference operator. It basically takes a pointer to a value and then returns the value. It returns the value of the variable located at the address specified by its operand. Continuing on with the parked cars analogy example , what if we wanted to know what the value stored at which car_ford was pointing at. This is where the “*” is applied to find the value pointed by.

car_hummer = *car_ford;

This will store the value of 50 in the car_hummer variable. Following our analogy, car_hummer will ask car_ford what value it is storing. It will show to be 500. The variable car_hummer will then go to the location of where the car is parked, which is 500 and enquire what the value is stored in that car. That car, which is the value car_ford, will show the value to be “50”. So car_hummer will now know to store 50 in his car.

pointers diagram 2

Share this post