Introduction to Pointers, Structures and Linked-Lists Part 1
Introduction to Pointers
by Chris Bennett aka Dwarfsoft


In this article I will be assuming that you have a reasonable understanding of C/C++. I will basically be covering a simple example of pointers and reference and dereferencing. From there we will go on to creating simple structures, followed by the combining of the two to create a linked list. Let us step into the code then shall we…

Pointers are a major part of the C/C++ programming language. They are used all of the time when you are passing complex and abstract data types from one function to the next. So what is a pointer? A pointer is basically a memory location that references another point in memory. But what does it actually mean?

Simple Memory
Figure 1 - A simple view of a chunk of memory

Above is a simple picture of a memory chunk, as the caption states. This small portion of memory is 8 bytes long (the number of squares). If I wanted to write something in the first box, then I would have to write to the memory address 0x00. Similarly, if I wanted to write to the last box then I would need to write to the memory address 0x07.

If I now say theoretically that I define a pointer and it just so happens to be located in 0x00 and took up only that square (this is a one-byte pointer) and I had the value 0x07 in that square, then the pointer would be referencing the memory address 0x07.

Pointer Referencing
Figure 2 - A simplified pointer

That was a theoretical look on a small scale (you actually have a LOT of squares, and a pointer is actually 4 bytes long), and a very low level one. These days, it is uncommon to need to use a direct link to a specific memory location. Most times you will just need to reference variables in your program. So how do you do that? First we need to look at how you define a pointer, and then we will look at referencing.

int *p,i;

The only thing difference between the definition of the variables 'p' and 'i' is the '*' character. This is what defines a variable as a pointer, what's more, the definition of a pointer also determines what type of variable the pointer shall point to. 'p' is a pointer to an integer. Using the above definition, we can now take a look at a reference.

p = &i;

The '&' character is referred to as 'address of' whenever it is seen by most programmers, because that is essentially what it is. Just using the simple variable 'p' (without modifiers) is just the spot in memory where the address of what the pointer 'p' references. In the above example this is essentially what is happening: the pointer 'p' is being assigned to point to the address of the integer 'i'.

Now that we understand the basics of pointer assignation, we can move on to the dereferencing of pointers.

*p = 5;

Figure 3 - *p is really *(&i) from this image

The code in the previous line sets the memory address that the pointer 'p' references to the value of 5. Basically, this piece of code will set the variable 'i' to the value 5. Try it, and see for yourself.

Cheers, Chris Bennett (a.k.a. Dwarfsoft)

Author: Chris Bennett aka Dwarfsoft
Novermber 26, 2000
© Copyright Chris Bennett, 2000-2001

Discuss this article in the forums

Date this article was posted to 9/30/2004
(Note that this date does not necessarily correspond to the date the article was written)

See Also:

© 1999-2011 All rights reserved. Terms of Use Privacy Policy
Comments? Questions? Feedback? Click here!