# 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;``

`t` is equivalent to `&t` and `&t`. 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,4};

// First cell of array
printf ("*tab     = tab = %d\n", *tab);
// Second cell of array
printf ("*(tab+1) = tab = %d\n", *(tab+1));``````
```*ptr     = tab = 2
*(ptr+1) = tab = 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

• declares an array of characters of 4 rows by 6 columns;
• displays the address of the array;
• displays the address of the array increased by 1.

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

```Before : 0x7ffddefd68f0
After  : 0x7ffddefd68f6
```

### Exercise 2

The following statements are provided:

``char text = "Hello", *ptr;``

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

• square brackets `[` and `]` ;
• the `string.h` library.

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?

What does the following code display?

``````short tab[]={1,2,3};
*(tab+2)=4;
printf ("tab = %d", tab);
``````

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");``````