Pointers in C

Published 2004-10-09. Read 3,771 times. 0 Comments. Tagged: c pointers programming security tutorial

C, on the whole, is a pretty easy language to understand and write but there is one topic that people get stuck on and often get scared away from a very powerful language. Pointers. Pointers are a method of pointing to a memory address. or series of memory addresses.

Lets take a time to 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 basically 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.

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:

will 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.

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 vars. 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 */

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.

About Oli: I’m a Django and Python programmer, occasional designer, Ubuntu member, Ask Ubuntu moderator and technical blogger. I occasionally like to rant about subjects I should probably learn more about but I usually mean well.

Stay updated: Subscribe by RSS or Subscribe by Email.