pointers

As you already know, whenever you declare a variable in C, memory is allocated to hold its value. To access that value, you simply refer to it by the variable name. You don't need to know exactly where the data is stored in memory. When you were programming in TOY, the opposite was true. You could store values in memory, but there were no named variables. To access the data, you referred to it directly by its address (a hex value between 0 and FF.) In C, pointers allow you to refer to data stored in memory by address instead of by variable name. The usefulness of this will become apparent when you learn about linked lists.


syntax

The syntax for declaring a pointer is:
type * var_name;

'type' is the base type of the pointer, which is the type of data the pointer can point to. 'Point to' is synonomous to 'holds the address of.' When we say, 'p points to x', we mean that the pointer p has the value of the address of x (where x is some data stored in memory).

The '*' indicates that you are declaring a pointer. The spacing surrounding the * doesn't matter.

The usual rules apply for var_name (letters, numbers, underscore.)


operators

There are two important operators for pointers:

& - returns the address of the variable it precedes

* - returns the value stored at the address it precedes (a.k.a. indirection, dereferencing op.)


examples

Here is some basic pointer code. An illustration of what memory would look like after each step is shown to the right.

int *p;
         
The pointer p is declared. Just like any other variable, p is allocated memory to hold its value (which has not yet been set.) In the picture to the right, p is allocated memory location 32.

      

int q;
         
Here, a new integer variable, q, is created. It has been allocated memory at location 34.

      

q = 100;
         
q is initialized with the value 100.

      

p = &q;
         
p is initialized with the value of the address of q. Since the variable q is stored at memory location 34, p will store the value 34.

      

printf("%d ", *p);
         
*p returns the value stored at the address p holds. Since p is 34, and mem[34] is 100, 100 is the value that will be printed.

Here's another way to visualize the relationship between p and q. This picture indicates that p 'points' to q:

In the same style as above, here's some more basic pointer code:

int *p;
         
The pointer p is declared. (Arbitrarily,) p has been given memory location 32.

      

int q;
         
Here, a new integer variable, q, is created. It has been allocated memory at location 34.

      

p = &q;
         
In this step, p has been set to 'point' to q. It doesn't matter that q hasn't yet been initialized.

      

*p = 199;
         
The address stored in p is used to access the variable q. Setting the data stored at address 34 has the same effect as initializing q directly.

      

printf("%d ", q);
         
By printing the value of q, you can confirm for yourself that q was actually initialized to 199 as indicated in the above picture

See also: arrays & pointers, pass-by-reference vs. pass-by-value, linked lists.