How to play with pointers in C


Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Given an array arr[ARRAY_SIZE] we can get the address of the i-th element by arr + i as arr works as a pointer to the first element of the array. This is true for any pointer pointing any element of arr array.

In the example above, p points to the 4th element of array by the assignment p = arr + 3. The statement *p = 55; modifies the contents of the memory location pointed to by p, essentially arr[3].

In the following example, all array elements are modified by pointer:

This is what happens in the example above. arr is initialized as follows:

arr:         1    2    3    4
address:     10   14   18   22

The address is hypothetical here. p points to the first element of arr by int32_t *p = arr;:

arr:         1    2    3    4
address:     10   14   18   22

A loop iterates ARRAY_SIZE times. In each iteration *p is multiplied by *p - the dereferenced value of p is squared - and stored in the memory location pointed to by p by the statement *p *= *p;. This modifies one array element in every iteration. The next statement, p++;, increments the pointer p, not by 1, but by sizeof(*p), equivalently sizeof(int32_t) - in this case 4 at a time.

In the hypothetical address above:

before 1st iteration: p = 10
after 1st iteration : p = 14
after 2nd iteration : p = 18
after 3rd iteration : p = 22
after 4th iteration : p = 26

This can be verified by the following example with 16 bit integer:

Notice that p is incremented by 2, or sizeof(int16_t), at every iteration.

Warning: When the loop exits in the above example, p points to one past the last element of arr. That area is outside the declared variables in the program. Accessing that location is undefined behaviour.

int16_t arr[ARRAY_SIZE] = { 0 };
int16_t *p = arr;

for (int i = 0; i < ARRAY_SIZE; i++)
    printf("%p\n", p);

/* printf("%d\n", *p); */ /* Fatal: p points to outside location of declared variable - undefined behaviour */
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content