Menu Close


C: Pointers & Arithmetic

C programming language is famous for its ability to access memory directly by address. For this C has a special data types named pointers. We already mentioned pointers before, in the article about functions. Next you will learn a bit more about this concept.

Pointer Variables

You define a pointer variables using prefix “*” in front of variable name. A pointer can be initialized with  value  NULL. This is a constant that is actually = 0. It represents the zero memory location of a computer that is hardware protected and you can not read value or write value on this location.


Variable Address

Address of a variable is extracted with symbol “&” and it can be assigned to a pointer. This is the most common way to initialize a pointer. Once you know the location of a variable you can read or write its content using the pointer instead of variable name.


Pointer Arithmetic

Sounds exotic but is not hard at all. There are two ways to use a pointer after it is defined. First way to use it as value (with prefix *). Second is to use it as address (without the prefix). Since a pointer is a number it can be printed and you can perform arithmetic operations with it.



Take a look at last digit of p. It is 4 then it is 8 then is back 4. That means we have changed the address where pointer p is pointing. We have incremented pointer with one, but we have got 4 numbers up. That is pointer arithmetic is “smart”. It will give you next address for “int” type. Since my machine is on 32 bit, int occupy 4 bytes. So the increment it will sow me position for the next memory location after my integer.

Increment & Decrement

There are two specific operators to C:

  • increment operator: ++
  • decrements operator.

You can use these two operators to perform increments and decrements of pointers. They will be type dependent for pointers. It will increment/decrements an address with the size of data type. If an address point to 4 bytes, the increment will be size of data type divided by 4.

Array Traversal

Arrays are defined in C as read only pointers to first element of array. So we can assign this pointer to another control pointer that is not read only. By using pointer arithmetic we can change control pointer to visit all elements of the array.



  • In previous example we initialize the array with elements using a literal;
  • The number of elements is automatically computed by C but we know is 10;
  • In for loop we use two increments: i++ and p++ separated by comma.

Comparing Pointers

Since the pointers are unsigned integer numbers, you can compare two pointers using relation operators. For array traversal you can detect if one element is precedent, current or next using < , = , > like in example below. 

Array of Pointers

This is a strange thing. You can have a collection of pointers. These pointers can point to first element into another collection. The most common array of pointers is *char[]. It represents actually and array of strings, like in the following example:

Now you know almost everything about pointers. So we can continue with some juicy stuff: 

Using pointers in functions

You can use pointers as input/output parameters to pass data by reference. Also you can use pointer as a result to return a group of values. Without pointers you can not return more than one single result, but using pointers you can return a structure or array.


  • Function name must have a pointer prefix: “*”,
  • The variable you are going to return must be “static”.


Static Variables

In previous example we have used keyword: “static” to define a special kind of variable. This variable is defined only once, when the function is first call. Second time the variable will not be re-defined. This variable is stored on the heap. Local variables are stored on the stack. 

You can not return a reference to a local variable. If you try you program will crash. It is because all variables that are allocated on the stack are purged from memory when the function returns. So the pointer is going to point to an invalid memory location.

Registry Variables

There is another classes of special variables called “register” variables. These are local variables that should be stored in a CPU registry instead of RAM. For this variable we can’t use operator ‘&’ to read memory location. Therefore we can’t have a pointer to a register kind of  variable.

The registry variables can be used for very fast access. It may be for example a counter. The compiler will try to create this kind of variable on registry but is not 100% guaranteed it will be. 

Pointer to Struct

Pointers can be used to refer to a struct by its address. This is particularly useful for passing structs to a function by reference or to refer to another instance of the struct type as a field.

The pointer value can be refereed just like any other pointer, using star operator: “*”.  There is also  a specific operator “->” which is referring to the value of a member of the struct.



Next article: Input & Output