copyright 2005 Mac-arena the Bored Zo.
This work is licensed under a Creative Commons Attribution 2.5 License.
this is regular text. this is a variable, some code
, and some sample output.
/*this is code.*/
this is output you'd see on your screen.
a pointer is a memory address.
(mmm, short paragraphs.)
say you declare a variable named foo.
int foo;
this variable occupies some memory. on a PowerPC, it occupies four bytes of memory (because an int
is four bytes wide).
now let's declare another variable.
int *foo_ptr = &foo;
foo_ptr is declared as a pointer to int
. we have initialised it to point to foo.
as I said, foo occupies some memory. its location in memory is called its address. &foo
is the address of foo (which is why &
is called the 'address-of operator').
think of every variable as a box. foo is a box that is sizeof(int)
bytes in size. the location of this box is its address. when you access the address, you actually access the contents of the box it points to.
this is true of all variables, regardless of type. in fact, grammatically speaking, there is no such thing as a 'pointer variable': all variables are the same. there are, however, variables with different types. foo's type is int
. foo_ptr's type is int *
. (thus, 'pointer variable' really means 'variable of a pointer type'.)
the point of that is that the pointer is not the variable! the pointer to foo is the contents of foo_ptr. you could put a different pointer in the foo_ptr box, and the box would still be foo_ptr. but it would no longer point to foo.
the pointer has a type, too, by the way. its type is int
. thus it is an 'int
pointer'. an int **
's type is int *
; the use of pointers to pointers is called multiple indirection.
int bar = *foo_ptr;
the dereference operator (prefix *
, not to be confused with the multiplication operator) looks up the value that exists at an address. (on the PowerPC, this called a 'load' operation.)
it's also possible to write to a dereference expression (the C way of saying this: a dereference expression is an lvalue, meaning that it can appear on the left side of an assignment):
*foo_ptr = 42; //sets foo to 42
(on the PowerPC, this is called a 'store' operation.)
here's a declaration of a three-int
array:
int array[] = { 45, 67, 89 };
this variable is an extra-big box: three int
s' worth of storage.
but here's a little secret: you can never refer to this array again.
'what?' you say. 'but the compiler lets me do that! watch!'
printf("%p\n", array); //prints some hexadecimal string like 0x12307734
ah, but what does %p
mean?
it means 'pointer'.
when you use the name of an array in your code, you actually use a pointer to its first element (in C terms, &array[0]
). this is called 'decaying': the array 'decays' to a pointer. any usage of array is equivalent to if array had been declared as a pointer (with the exception that array is not an lvalue: you can't assign to it or increment or decrement it, like you can with a real pointer variable).
so when you passed array to printf
, you really passed a pointer to its first element, because the array decays to a pointer.
say we want to print out all three elements of array.
int *array_ptr = array; printf(" first element: %i\n", *(array_ptr++)); printf("second element: %i\n", *(array_ptr++)); printf(" third element: %i\n", *array_ptr);
first element: 45 second element: 67 third element: 89
you're probably familiar with the ++
operator. it adds 1 to a variable, the same as variable += 1
. but what did we do here?
well, the type of a pointer matters. the type of the pointer here is int
. when you add to or subtract from a pointer, the amount by which you do that is multiplied by the size of the type of the pointer. in the case of our three increments, each 1 that you added was multiplied by sizeof(int)
.
incidentally, though sizeof(void)
is illegal, void
pointers are incremented or decremented by 1 byte.
printf("%i\n", array[0]);
OK… what just happened?
this happened:
45
well, you probably figured that. but what does this have to do with pointers?
this is another one of those secrets of C. the index operator (e.g. array[0]
) has nothing to do with arrays.
oh, sure, that's its most common usage. but remember that arrays decay to pointers. that's a pointer you passed to that operator, not an array.
as evidence, I submit:
int array[] = { 45, 67, 89 }; int *array_ptr = &array[1]; printf("%i\n", array_ptr[1]);
89
that one might bend the brain a little. here's a diagram:
array points to the first element of the array; array_ptr is set to &array[1]
, so it points to the second element of the array. so array_ptr[1]
is equivalent to array[2]
(array_ptr starts at the second element of the array, so the second element of array_ptr is the third element of the array).
also, you might notice that because the first element is sizeof(int)
bytes wide (being an int
), the second element is sizeof(int)
bytes forward of the start of the array. you are correct: array[1]
is equivalent to *(array + 1)
. (remember that the number added to or subtracted from a pointer is multiplied by the size of the pointer's type, so that '1
' adds sizeof(int)
bytes to the pointer value.)
this document is also available in zip format.