Menu Close

Articles

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.

Example:

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.

Output:

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.

Example:

Output:

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.

Example:

Notes:

  • 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: 

Pointer as result

You can use a pointer as a result of a function. Without pointers you can not return more than one single result, but using a pointer you can return a structure or array. There is a catch, the pointer or the structure must be allocated on the heap. A local array that is not dynamic allocated is on the stack and can not be returned as a result.

Rules:

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

Example:

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.

Example:

 

Next article: Input & Output