This is the big one. Unfortunately, to really understand how pointers work and how to use them correctly we need a little background information about C's environment. Bear with me as we talk about some stuff you (probably) already know -- I promise that when we get back to talking about pointers, it'll all be clear. Or at least less muddy.

When we declare a variable in C, what we're really asking the compiler to do is assign a name to a piece of memory so that we can access it easily. Depending on the type of our variable (int, char, float, etc.) the compiler will allocate different amounts of space. For an integer, we get 4 bytes (1 byte = 8 bits, so 4 bytes is 32 bits); for a character we only get 1 byte (8 bits). The size of data types can actually vary between compilers, but don't worry about that for now.

The important thing here is that each piece of memory we get, regardless of its size, has an address in memory. When we say something like:

int foo;
foo = 42;

The compiler looks up the address of foo (which is some cryptic hexadecimal number like 0xbf891fb0) and stores the number 42 into the 4 bytes of memory beginning at that address. If we wanted to find out for ourselves where foo is stored, we use the & operator, which gives the address at which a variable is stored:

int foo;
foo = 42;

printf("%p\n",&foo); // Prints '0xbf891fb0'

We can also have pointer variables (ah-ha!) which store addresses in memory. For example, we could store the address of foo with an integer pointer (of type int * -- the * means 'pointer'):

int foo;
int *p_foo;

foo = 42;
p_foo = &foo;
printf("%p\n",p_foo); // Prints '0xbf891fb0'

That's all well and good, but what if we want to use our pointer? To find out what it points to, we need to dereference it using the !* operator. This is completely different from the int * we used to declare the variable; there, we used the star (*) to indicate that our variable was a pointer to an integer rather than an integer itself. Let's go back to our example:

int foo;
int *p_foo;

foo = 42;
p_foo = &foo;

printf("%p\n",p_foo);  // Prints '0xbf891fb0'
printf("%d\n",*p_foo); // Prints '42'
printf("%p\n",&foo);   // Prints '0xbf891fb0'
printf("%d\n",*&foo);  // Prints '42'

printf("%p\n",&p_foo); // Prints '0xbfcec3fc' -- Different!

Let's slow down for a minute and think about where we are. foo is an integer, located at some address (0xbf891fb0), with value 42. p_foo is also an integer, located at some other address, with value 0xbf891fb0. But because p_foo is an integer pointer (int *) rather than just an integer (int), the compiler knows to treat its value as an address in memory. When we dereferences p_foo using the * operator, what we said was "look up the address stored in p_foo and return the data stored at that address." Because we had previously stored the address of foo into p_foo using the & operator, p_foo points to foo, and *p_foo gives us the value of foo.

You can think of *p_foo as being identical to foo -- changing the value of one will change the value of the other. The difference is that we can change which piece of data p_foo points to, whereas foo will always point to those same 4 bytes we originally allocated.

-- TrevorFountain - 05 Dec 2008

A quick summary and a video clip on pointers

As a quick summary, there are just 3 simple things that you need to take away about Pointers:

*1) How to declare a pointer variable

*2) How to make it to point to something (Lets call this something as Pointee, the thing that is being pointed to )

*3) How to access the pointee, often called Dereferencing (i.e how to get/change the contents of the Pointee)

Really, that is all there is to pointers.

Drawing pictures of an imaginary cell in the memory of the pointee and your pointer can sometimes be a good aid to understand and debug.

Here is a link to a nice little video that can help to reinforce these things about pointers. Enjoy! http://cslibrary.stanford.edu/104/

-- Main.s0199805 - 12 Feb 2009

Topic revision: r2 - 12 Feb 2009 - 12:21:19 - SripriyaG
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback
This Wiki uses Cookies