Introduction to Pointers

Tutorial Article for all Programming Languages Using the Pointer

© Guy Lecky-Thompson

A tutorial article on pointers without restriction to a specific programming language with examples in C/C++, Modula-2 and BASIC.

Introduction

This article is a general beginner's guide to pointers and pointer arithmetic. We have tried to keep it language-independent and offer notation examples for 3 languages:

This mix was chosen because the 3 main types of notation for referencing and de-referencing pointers are covered in these 3 languages. Understanding pointers is much more important than the notation - their use can be adapted to almost any language that the reader may come into contact with.

What is a Pointer?

A pointer is a reference to a point in memory. It is like the address to a given place where the programmer can store data or code to be executed. To use a pointer, we need first to declare it, and then to dereference it in order to access the data.

So, in Modula-2 we write:

VAR myNumber : POINTER TO INTEGER;

The same declaration in C looks like:

int * myNumber;

Finally, in BASIC style languages, we only have the possibility to declare a variable and then obtain the address reference to it. Usually, this 'address of' operator is the ^ symbol:

myNumber% = 42
myNumberAddress = ^myNumber%

In VB (Visual Basic), we can only obtain a pointer to memory by allocating the memory ourselves and using the value returned by HeapAlloc as a reference to the memory area in question. This is highly flexible, but quite cumbersome.

Accessing Pointer Data

To access the data pointed to in a C program, we use the dereference operator, * as in:

*myNumber = 42;

The equivalent in Modula-2 is:

myNumber^ := 42

In BASIC languages, the declaration of the pointer requires that the variable is initialized, and while we can change the data afterwards using the indirection operator, there is no specific dereferencing in the same way as in Modula-2 and C style languages.

Pointers to Functions

The above examples all used pointers to data, but we can also construct pointers to executable code, or functions. This is useful for allowing callback functions in Windows programming. It is also useful if we wish to use dynamic programming techniques in which we construct the code in memory and execute it during the life of the program.

In order to actually make use of this feature, we also need to be sure that we have supplied appropriate memory blocks containing any parameters that must be fed to the function before control is transferred to it. This advanced use of functions is best applied when the language, or programming environment supports it directly.

Using Pointers

Aside from using pointers to access functions, they can be very useful in creating dynamic data structures, such as arrays, stacks and lists. A Linked List, for example, uses pointers to each node in order to dynamically allocate the memory and retain a reference to it.

We can also use pointers in preference to arrays (otherwise known as dynamic arrays). This can be useful to build arrays that are larger than the stack size allowed by the language (a common problem in Modula-2), or to create arrays where we do not know the size at the start of the program.

Most languages also support pointer arithmetic, required if we wish to access the elements in the block of memory as if it were an array. Pointer arithmetic allows us to step through the elements by a given amount : the amount being equal to the size of the element being referenced. This automatic feature in C allows us to declare a user-defined type (such as a structure), allocate a block of memory to store a collection of them in, and then step through the array by incrementing the pointer.

If we rewrite the code, and make the struct bigger, then the code to allocate the memory, and the pointer arithmetic will still work, because it is a feature of the C language. This memory handling trick means that, if we use pointers, much of the data processing code that we need for an application can be re-used even if we change the nature of the data being stored.

Further Reading


The copyright of the article Introduction to Pointers in Computer Programming is owned by Guy Lecky-Thompson. Permission to republish Introduction to Pointers must be granted by the author in writing.




Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo