Oli Warner About Contact Oli on Twitter Subscribe

Pointers in C

Saturday, 9 October 2004 c pointers programming security tutorial

C is a pretty easy language to understand but there is one topic that scares the daylights out of beginners: pointers.

Pointers are a method of pointing to a memory address. Simple! Let’s go home! Okay, fine, let’s go slowly and take a look at arrays.

What is an array in C? Its a block of adjacent memory, split into chunks depending on the size of data-type. For example if we have a char array, of size 10:

char charVar[10];

This means there are 10 blocks of 4byte memory which will allow chars to be put in them. You can get from block to bock by addressing them like: charVar[6], which will give you the seventh block’s address, and the data within. You’ll note that indexing starts from 0.

There is another way of doing it. If you hadn’t noticed arrays and pointers are heavily entwined. You could:

#include <malloc.h>;
char * charVar;
charVar = malloc(sizeof(char)*10);

This will give us the same result as the first example. We now have memory which we can address. Even though we didnt define it as an array, we can address it as one:

charVar[6]

… and you’ll still access the 7th item. So that’s all good. We know we can address pointers, but what’s the point?

Having a pointer that we can address like this means we can resize it dynamically in memory, rather than having a static memory array.

charVar = realloc(charVar, sizeof(char)*newInt);

Where newInt is the value you need the ‘array’ resizing to.

For a start this gives us so much more power than something like java where you either have to make a new array of the right size, or use a library object like a Vector Object which usually isnt ideal. We can make our array bigger or smaller as we need. (Ed: Note the original date on this page. Java is much better now.)

We can also pass this pointer, which is just a memory address for the start of the array remember, to functions so they can use them, Lots of the “string” (a char array) functions pass pointers rather than actual variables. This makes the program a lot leaner when it runs and also uses a lot fewer resources when running.

When you pass the pointer, as I’ve said, you’re passing a reference to first block of memory. The funtion you’re passing it to can then tick through the pointer as you need. Here’s a short example of how this could help you make better programs. It’s just a way of how a function can not only read from a pointer but write to it for output.

#include <malloc.h>

void fillPointer(char * output, char * input, int length) {
memcpy(output, input, sizeof(char)*length);
}

int main() {
char pie[9] = "chocolate";
char * charResult;

/* Make charResult bigger as it currently doesnt hold anything */
charResult = malloc(sizeof(Char)*9);
void fillPointer(charResult, pie, 9);

/* charResult is now the same as pie */

/* now freee the memory that charResult take up */
free(charResult);
}

Sure that may seem very insignificant, but you’re able to do this with all your pointer types. If you have a very heavy object you dont want to be passing it around all the time, so you can pass it by reference.

You’ll notice that the last line contains the “free” funtion. because we asked for something to have its memory explicitly assigned, we have to explicitly give it back to the system… by calling the free(var) function.