Thursday, August 16, 2012

Pointers


Pointers. 


A pointer is a variable whose value is an address.

                  int thing;            /* define a thing */
int *thing_ptr;   /* define a pointer to a thing */ 


There are things and pointers to things. Knowing the difference between the two is very important.
The address of thing is 0x1000. Addresses are automatically assigned by the C compiler to every variable. Normally, you don't have to worry about the addresses of variables, but you should understand that they're there.
Our pointer (thing_ptr) points to the variable thing. Pointers are also called address variables because they contain the addresses of other variables. In this case, our pointer contains the address 0x1000.




Pointer Operator Syntax. 


The operator ampersand (&) returns the address of a thing which is a pointer. The operator asterisk (*) returns the object to which a pointer points. These operators can easily cause confusion.