Lesson 11.4. Incrementing pointers

Explanation

When a pointer is incremented (ptr+1) its address is advanced by the number of bytes of the pointed type. Let's consider the (classical) size of the following types :

Type Size
short 2 bytes
long 8 bytes

The following code increases the pointed address by 2 bytes:

short *ptr;
...
// Increases the pointed address by 2 bytes (sizeof(short))
ptr++;

The following code increases the pointed address by 8 bytes:

long *ptr;
...
// Increases the pointed address by 8 bytes (sizeof(long))
ptr++;

Example

The following code displays the address of 4 pointers (*char, *short, *int and *long) before and after incrementation.

printf ("Before, *char  = %p\n",   pChar);
printf ("After, *char  = %p\n\n", pChar+1);

printf ("Before, *short = %p\n",   pShort);
printf ("After, *short = %p\n\n", pShort+1);

printf ("Before, *int   = %p\n",   pInt);
printf ("After, *int   = %p\n\n", pInt+1);

printf ("Before, *long  = %p\n",   pLong);
printf ("After, *long  = %p\n\n", pLong+1);

We check that the addresses are increased by the size of the pointed type:

Before, *char  = 0x7ffd8f06531b
After, *char  = 0x7ffd8f06531c (+1 byte)

Before, *short = 0x7ffd8f06530e
After, *short = 0x7ffd8f065310 (+2 bytes)

Before, *int   = 0x7ffd8f0652fc
After, *int   = 0x7ffd8f065300 (+4 bytes)

Before, *long  = 0x7ffd8f0652e8
After, *long  = 0x7ffd8f0652f0 (+8 bytes)

Array pointers

The name of an array used without braces is a pointer to the first cell of the array. Consider the following statement:

int t[10];

t is equivalent to &t and &t[0]. This is the reason why tab1=tab2 does not copy the array tab2 into the array tab1. This type of assessment should not be used.

For arrays, the concept remains the same: we increase by the size of the pointed type. For example:

// 2 cells array
int tab[2] = {2,4};

// First cell of array
printf ("*tab     = tab[0] = %d\n", *tab);
// Second cell of array
printf ("*(tab+1) = tab[1] = %d\n", *(tab+1));
*ptr     = tab[0] = 2
*(ptr+1) = tab[1] = 4

We can see that the pointer, increased by 1, addresses the second cell of the array.

However, there is a subtlety for multidimensional arrays: the pointer advances by one in the main dimension (typically a row), not by one cell.

Exercises

Exercise 1

Write a program that

By how many bytes has the pointer advanced? What to conclude ?

Before : 0x7ffddefd68f0
After  : 0x7ffddefd68f6

Exercise 2

The following statements are provided:

char text[10] = "Hello", *ptr;

Display each letter of text separated by a tab. You must not use:

Use only pointers. Here is the expected result:

H   e   l   l   o

Quiz

By how many bytes does a pointer incremented by 1 increase?

Check Bravo! Pointer addresses are increased by the number of bytes of the pointed type. Try again...

What does the following code display?

short tab[]={1,2,3};
*(tab+2)=4;
printf ("tab[2] = %d", tab[2]);
Check Bravo! We move forward two cells in the assignment. Try again...

Regarding the following program:

short tab1[]={1,2,3}, tab2[]={1,2,3};

if (tab1==tab2)
  printf ("Same\n");
else 
  printf ("Not the same\n");
Check Bravo! Here we do not compare arrays, but their addresses. The name of an array without the brackets is the address of its first cell. Try again...

See also


Last update : 11/30/2022