Pointers

Are declared thus:

[attributes] type* name;

name refers to the value of the pointer variable, which should be the address of a variable of type type.

*name refers to the value of the variable of type type, pointed to by the address in the pointer variable.

Pointers may only have addition and subtraction operations performed on them, which are always calculated in the units of the size of their type.

Pointers to functions are declared thus:

[attributes] return_type (* var_name) (arguments);

And the pointed to function can be called thus:

result = var_name(arguments);

Pointer Example

The declarations:

short int IntegerArray [4];
short int* IntegerPtr;

might result in the following addresses being assigned:

Address Name
0x1000, 0x1001 IntegerArray [0]
0x1002, 0x1003 IntegerArray [1]
0x1004, 0x1005 IntegerArray [2]
0x1006, 0x1007 IntegerArray [3]
0x1008, 0x1009 IntegerPtr

The following code could then be written:

IntegerPtr = &IntegerArray [0];

The base address of IntegerArray (0x1000) is stored in IntegerPtr (at addresses 0x1006 and 0x1007).

*(IntegerPtr + 3) = 12;

Each short int takes 2 bytes, so 2 * 3 = 6 is added to the address stored in IntegerPtr (0x1000) to give an address of 0x1006. The value 12 (0x000C) is then stored at addresses 0x1006 and 0x1007 (IntegerArray [3]).