NOTE: This is just because a lot of beginners need a grasp on this concept.
WHAT IT IS
----------
Pointers are addresses. The can be assigned, or made to "point to" another memory location. They are a powerful concept that is not well understood by many users of C. Their use can make code more efficient, more readable, and more functional.
HOW TO USE IT
-------------
Pointers are declared by using the following template (assuming T is a type):
Of course, some prefer the convention of:
but these modifications are in the personal opinion of the user, since the compiler ignores the whitespaces in a declaration (that don't seperate a type and the first variable).
The address of a normal variable can be assigned to a pointer as follows:
1 2 3
|
T *ptr;
T var;
ptr=&var;
| |
This works because the ampersand (&) will take the address of var, and assign the value to the pointer, which itself holds an address.
Pointers to pointers can be made. And so can pointers to those, but they are hardly useful.
The asterisk (*) can dereference a pointer.
1 2 3 4 5 6
|
T *ptr;
T var, var2;
ptr=&var;
var2=*ptr;
*ptr+=var2;
| |
The last two statements effectively assign var2 the value of var (because the dereference operator tells the machine to follow the pointer to the variable), then adds var2 to var (the same as doubling var). Since the pointer was dereferenced to the same location as var, all operations on the dereferenced pointer will take effect on the variable it points to.
Often, to signify the state of not pointing to anything, a pointer will be assigned the value of 0. This pointer is the null pointer. It is illegal to dereference a null pointer.
Pointers can be declared constant by putting the keyword const before the asterisk in the declaration:
Do NOT confuse this with
which defines var to point to a type of constant T. Constant pointers must be initialized. Arrays, in fact, are constant pointers.
The space allocated for pointers is the space necessary to hold a memory address. This is usually an unsigned long.
Pointers can be redirected to multiple targets. For example:
1 2 3 4 5 6 7 8
|
T *ptr;
T var, var2;
ptr=&var;
//Do some stuff with var
ptr=&var2;
//Do some stuff with var2
| |
Pointers have their own kind of arithmetic: adding or subtracting values (let's say X) actually causes a shift in the address equal to X*sizeof(T). This is how arrays dereference (the expression a[x] is really *(a+x), which works because arrays are constant pointers). Multiplication and division are not allowed.
A pointer to any arbitrary type T must have the declaration T * OR one of the special types, void *. Void pointers (not to be confused with null pointers) can be assigned an address to any value. Because of this, pointer arithmetic on void pointers is illegal, as is dereferencing them. You can dereference and perform arithmetic on a casted void pointer, though.
A pointer definition doesn't implicitly span to each member in a declaration, you need an asterisk before each pointer variable.
WHY YOU NEED THEM
-----------------
ARRAYS, ARRAYS, ARRAYS!!! Every array is a constant pointer, so it is pretty crucial to have this feature.
Functions normally are passed copies of their parameters. By passing addresses (and defining the function to receive a pointer), the caller's value can be modified. Passing of this type is called pass by reference.
Pointers are crucial to the processing of strings. Every C String is a character array, and hence a constant pointer to a character.
They are also necessary when making linked lists and using dynamic memory allocation. Using pointers for dynamic memory allocation means you can:
-use the same pointer for multiple seperate memory spaces.
-assign this value to any other pointer with the same type or a void *.
-Define an arbitrarily long range to be put under a single pointer. This is the basis of arrays.
GOING FARTHER
-------------
Go ahead, try a few pointings. Remember, you have to cast a void pointer to use it.
Pointers are used internally on several occasions. Not a program doesn't have a pointer in it, because functions are pointers to the compiled code for that function in their data segment (yes, even main). Find other such uses.
Your reply is valued! Please give questions and comments below!
Thank you,
Graham Northup