Gw Temp


Tutorial - 'Advanced C++ Concepts Tutorial - Pointers' by AzureFenrir

An item about Programming Languages posted on


One part of AzureFenrir's Advanced C++ series, this tutorial shows you the basics of the mysterious C++ pointers.


Advanced C++ Concepts Tutorial - Pointers

AzureFenrir hath come! I bring thee another C++ tutorial! Bow to the immortal power of C++ pointers! MWAHAHAHAHAHAHA!!!

All right, all right, all right. I'll begin without further hesitation.

The concept of pointers is, admittingly, a difficult C++ conecpt for newer C++ programmers who may have understood things like variables and loops. However, the importance of pointers in C++ can't be stressed enough, so here's a tutorial to help explain the concept.

As you may know, everything that is stored in memory blocks. For example:

long A

May be stored in memory like this:

Because A is a long variable, it takes up four bytes (represented as blocks) of memory. Other blocks of memory may be used to store other things, such as the character 'S' or the variable X.

Memory blocks need a way to be identified, so that programs could quickly find the variables it stored. To do this, each memory block is assigned a number based on its position in memory. Thus, let us assume that variable A was stored in the 350th memory block. Then:

So A is stored in memory position 350 and takes up the blocks 350, 351, 352, and 353. Thus, whenever you access the variable A, you access memory position 350.

NOTE: Remember that the memory position that a variable occupies will change every your program is opened. Therefore, Variable A may occupy memory block 350 the first time you open the program, and 5013 the second time.

So far, our variables directly reference a memory position. We have no control over which memory position it occupies, and cannot change its position in memory. However, with something called Pointers, we could.

Pointers, like normal variables, can store a value. However, points are unique in that it stores a value that points to another memory address. Thus, if a pointer contains the value 350:

Then it would point to the value in the memory address 350:

Which contains 154276. Because pointers store a MEMORY ADDRESS instead of a VALUE in its memory position, the memory address that it points to CAN be changed. This means that one pointer can point to any memory address in your program.

You declare a pointer like this:

type *Variable_Name;

The asterisk (*) denotes that the variable should be declared as a pointer. Without the (*), the variable will simply be a normal variable instead of a pointer.

So, before you use a pointer, you have to set it to a memory address. Otherwise, it will contain an invalid memory block, can guess what happens next. No...the computer won't blow up, but you will probably end up messing with memory blocks that you shouldn't mess with and possibly crash your computer.

To set a pointer to a memory address, you need to reserve a memory address for a variable first:

int *pointer;
int Variable = 0;

Now, you can have pointer point to Variable by using the & operator:

pointer = &Variable;

The & operator obtains the MEMORY ADDRESS of a variable, which is then stored into the pointer.

However, a pointer would be useless if you can't access and manipulate the value that is stored in it, right? So, how do you change the actual value that a pointer points to? Can you guess?

Well, if you said something like:

pointer = 354;

Guess what? You're wrong!!!! MWAHAHAHAHAHA!

Ahem. Anyway, the above code would actually store the memory address "354" into a pointer, which means that pointer will now point to memory block 354...which is not good.

To actually store stuff into the value that a pointer points to, you have to use another operator, the * operator:

*pointer = 354;

The * operator refers to the actual value that the pointer points to. It basically makes the pointer act like a regular variable. So, let us say that pointer A points to variable B, at the address 350:

In this case, *A would be the value at memory block 350, and A (without the asterisk (*)) would refer to the value at memory block 178.

I hope you understood all of this so far.

Pointers do not have to point to existing values. In fact, you could reserve new memory space for a pointer:

pointer = new int;

This code would create a new integer and store its memory address in pointer. When you no longer need the new integer, you should free the memory that is occupied using the delete keyword:

delete pointer

This would delete the new integer and free the memory blocks for other programs to use. Note that delete DOES NOT delete the pointer itself - it just deletes the variable that pointer points to. Thus, you can reuse pointer by having it point to another value.

You might think that pointers are useless now. However, pointers are actually very important, especially in C++ structures such as strings, dynamic lists, linked lists, trees, and many more. Both API and MFC Visual C++ also use pointers frequently.

There we go. Tutorial end. (Rings Gong)