C Tutorial
 Back to Tutorials Next Lesson

 Lesson 4: Introduction to Pointers

Lesson Requirements
A C compiler.

Lesson Summary
Moving on from Lesson 3: Strings and I/O, we come to an interesting topic called Pointers.

But First!
Before we start, let's cover a solution to the last exercise. If you recall, the exercise was to read 10 integers from the console, and print out the largest. Here is one way you might have solved this:

#include <stdio.h>

int Max(int a, int b)
{
if (a > b)
return a;
else
return b;
}

int main(int argc, char *argv[])
{
int num;
int largest_so_far;
int i;

printf("Please input your 10 numbers, in the form: 1 2 3 4...\n");
scanf("%d %d %d %d %d %d %d %d %d %d", &num, &num, &num, &num, &num, &num, &num, &num, &num, &num);

/* Now find the maximum number */
largest_so_far = num;
for (i = 1; i < 10; i = i + 1)
{
largest_so_far = Max(largest_so_far, num[i]);
}

/* Print out the maximum number */
printf("Largest Number was: %d", largest_so_far);
}

There isn't anything new here, however from now on we shall replace "i = i + 1" with the post increment operator "++" (i.e. "i++"). In C this means exactly the same as "i = i + 1". In this case we want to use the "post" increment (i.e. add one to i, after each iteration of the loop has finished. This is opposed to the "pre" increment operator, which adds one before the iteration).

Now would be a good time to go back over the entire series of tutorials and make sure you understand them.

Pointers
All the information we store in a computer is stored in it's memory, and we need a way of keeping track of where this information is stored. The way we do this is by using Pointers. Pointers are essentially variables which "point" to another variable (a location in memory, i.e. the 64th byte). You could think of Pointers as being similar to your house number on the street you live in.

How do we declare a pointer? A pointer variable is declared as:

var_type *var_name;

which declares a variable called 'var_name' of type 'var_type'. For example:

char *mychar;

declares a pointer to a character.

NOTE: Initially the pointer may not be setup (i.e. it could point to anything). More importantly, we have only allocated memory for the pointer, and not for the character we wish it to point to. We allocate that memory using the function malloc(), which is prototyped as:

void *malloc(int size);
// NOTE: This is a simplified version!

We have introduced a new type called "void", which essentially means it doesn't have a type. We'll return to this later. The malloc() function allows us to allocate memory as we need it (i.e. dynamic memory allocation). The parameter "size" allows us to specifiy the amount of memory we wish to allocate -- a point to the beginning of the block is returned.

This example uses a pointer to an integer:

// declare variable
int *myint;

// allocate memory
myint = (int *) malloc(sizeof(int));

// set the value of the memory location pointed to by myint:
*myint = 1;

Some new things here: Firstly the "(int *)" is used before the malloc(). This is known as a typecast, which essentially is telling the compiler what type the malloc() is going to return (in this case it is a pointer to an integer). "sizeof(int)" is the parameter being used, and is simpler for portablility and for use of structures which we shall cover at a later stage.

Finally, we use "*" to access the memory that the pointer is pointing to.

Well that's all for this Lesson. It is recommended that you read more on Pointers in a good C reference book.

 Back to Tutorials Next Lesson

Copyright © 1997, John Crickett & Neil Henderson.
Legal Information