C++ Pointers

C++ Pointers

Introduction to C++ pointers and their applications in C++ programs

Nov 20, 20174 min read

Overview:

You just started learning C++ programming and have a grasp on the basics and can write simple programs, but you’re stuck on C++ pointers because you don’t understand what it is or how to use it? Well, let’s fix that by learning the basics of pointers and how to apply them in C++ programs.

What is a pointer?

The first thing we need to be familiar with is the definition of a pointer.

A pointer is a variable whose value is the physical address of another variable.

Wait… physical address?

Yes. The physical address is the address in memory where the variable is currently stored. It looks something like this: 0x7fff5af67348. Don’t worry though, it is just a numerical value.

So why do I need a pointer?

Pointers are elemental to more advanced data structures, like an array or a struct. But we’re getting way ahead of ourselves. First, we have to learn how to declare pointers and also about some special operators that help us use pointers. Your question will be answered along the way.

How to declare pointers?

Like any other variable in C++, pointer variables also need a specific data type. The declaration of pointers follow this syntax:

type * name;

Some examples are:

int * number; char * character; double * decimal;

Well, you get the idea.

Wait, what's up with the asterisk?

The asterisk is a unary operator that is required to tell the compiler that a pointer variable is being declared. Without it, the variable is just a regular variable. One could say without the asterisk, the variable would be pointless.

-_-

Anyway, now we know how to declare a pointer. Next step is to learn about the operators that make pointers special. They are:

Address-of operator( & ) and Dereference operator( * )

Wait I've seen them before.

Well yeah, we were just talking about the asterisk. However, the use case of these operators with pointers do not affect their use with other operations. For now, we need to understand what they do and how to use them specifically with pointers.

What is the Address-of operator( & )?

The ampersand sign( & ), a.k.a the address-of operator is used to obtain the address of a variable. This can be done by preceding the name of any variable we need the address of with ( & ). If we assign the value to a new variable, then the new variable is our pointer.

int * foo = &bar;

In the example above, we are assigning the address of the variable bar to a pointer variable foo.

Alright, now this is the part where you should have asked why it's useful.

As we now know, pointers can store the address of another variable. However, pointers are special because they also have access to the values pointed to by the pointer variable.

I am getting more confused.

Don't worry. You’re confused because you're paying attention. Let's look at the above example again but with a value assigned to bar.

int bar = 2; int * foo = &bar;

Here, foo stores the address of bar, which probably looks something like 0x7fff5af67348. Another feature of pointers is that it can access the value pointed to by foo, which is the actual value of bar. We can do that by using a new operator.

What is the Dereference operator( * )?

The asterisk sign( * ) a.k.a. the dereference operator is used to obtain the value pointed to by a variable. This can be done by preceding the name of a pointer variable with ( * ).

int baz = *foo;

Here, baz now holds the value pointed to by foo, which is the actual value of bar in the first example. Which means now the value of baz is 2.

Wait that's not a new operator. It's what we used to declare a pointer.

Accurate observation. The asterisk that we used to declare a pointer is the same as the dereference operator. Nevertheless, the use case differs when we are trying to dereference a pointer variable. It is like a homonym.

Okay... So a pointer can store the address of another variable and can also access the value of that variable.

Yes perfect. Now you are ready for a fun example.

Fun example:

#include <iostream> using namespace std; int main() { int firstvalue, secondvalue; int * mypointer; mypointer = &firstvalue; *mypointer = 10; mypointer = &secondvalue; *mypointer = 20; cout << "firstvalue: " << firstvalue << "\n"; cout << "secondvalue: " << secondvalue << "\n"; return 0; }

So, the essence of this program lies from lines 8 to 11. Let’s look at each line in more detail:

int baz = *foo;

The address of the integer variable firstvalue is being assigned to a pointer variable mypointer.

*mypointer = 10;

The value pointed to by mypointer is being assigned the integer value 10.

Wait what?

Exactly. Up until now we knew that we could read the address and the value of a variable through a pointer with it’s address-of operator and dereference operator respectively. However, the dereference operator can also be used to write and reassign the values pointed to by the pointer.

But that’s not all…

mypointer = &secondvalue;

Here, mypointer is being assigned the address of a new variable, secondvalue Like any other variable in C++, a pointer variable can also be reassigned a new value, or in this case, an address of a new variable. Reassigning gives mypointer control of secondvalue, which means that mypointer can access and reassign secondvalue. This should make line 11 fairly obvious. The program above produces the following output.

first_value: 10 second_value: 20

In a nutshell:

A pointer is a variable that can store a physical address of another variable. A pointer has access to the value pointed to by the variable and also the ability to reassign it’s value. Furthermore, the pointer itself can be reassigned a new variable. Pointers are effectually elemental to more advanced data structures.