# Pointer Arithmetic in C

You should know by now that a pointer is nothing more than a variable used to store a memory address. If this is still news to you then go back and read Pointer Basics before continuing with this chapter.

In this chapter, we will discuss arithmetic operations that can be performed on pointers.

We can’t perform every type of arithmetic operations with pointers. Pointer arithmetic is slightly different from arithmetic we normally use in our daily life. The only valid arithmetic operations applicable on pointers are:

1. Addition of integer to a pointer
2. Subtraction of integer to a pointer
3. Subtracting two pointers of the same type

The pointer arithmetic is performed relative to the base type of the pointer. For example, if we have an integer pointer `ip` which contains address `1000`, then on incrementing it by `1`, we will get `1004` (i.e `1000 + 1 * 4`) instead of `1001` because the size of the `int` data type is `4` bytes. If we had been using a system where the size of `int` is `2` bytes then we would get `1002` ( i.e `1000 + 1 * 2` ).

Similarly, on decrementing it we will get `996` (i.e `1000 - 1 * 4`) instead of `999`.

So, the expression `ip + 4` will point to the address `1016` (i.e `1000 + 4 * 4` ).

Let’s take some more examples.

Suppose the address of `i`, `d` and `ch` are `1000`, `2000`, `3000` respectively, therefore `ip`, `dp` and `cp` are at `1000`, `2000`, `3000` initially.

## Pointer Arithmetic on Integers

Pointer Expression How it is evaluated ?
`ip = ip + 1` `ip` => `ip + 1` => `1000 + 1*4` => `1004`
`ip++` or `++ip` `ip++` => `ip + 1` => `1004 + 1*4` => `1008`
`ip = ip + 5` `ip` => `ip + 5` => `1008 + 5*4` => `1028`
`ip = ip - 2` `ip` => `ip - 2` => `1028 - 2*4` => `1020`
`ip--` or `--ip` `ip` => `ip + 2` => `1020 + 2*4` => `1028`

## Pointer Arithmetic on Float

Pointer Expression How it is evaluated ?
`dp + 1` `dp = dp + 1` => `2000 + 1*8` => `2008`
`dp++` or `++dp` `dp++` => `dp+1` => `2008+1*8` => `2016`
`dp = dp + 5` `dp` => `dp + 5` => `2016+5*8` => `2056`
`dp = dp - 2` `dp` => `dp - 2` => `2056-2*8` => `2040`
`dp--` or `--dp` `dp` =>` dp - 1` => `2040-1*8` => `2032`

## Pointer Arithmetic on Characters

Pointer Expression How it is evaluated ?
`cp + 1` `cp = cp + 1` => `3000 + 1*1` => `3001`
`cp++` or `++cp` `cp` => `cp + 1` => `3001 + 1*1` => `3002`
`cp = cp + 5` `cp` => `cp + 5` => `3002 + 5*1` => `3007`
`cp = cp - 2` `cp` => `cp + 5` => `3007 - 2*1` => `3005`
`cp--` or `--cp` `cp` => `cp + 2` => `3005 - 1*1` => `3004`

Note: When we increment or decrement pointer variables using pointer arithmetic then, the address of variables `i` , `d` , `ch` are not affected in any way.

Arithmetic operation on type `char` seems like ordinary arithmetic because the size of `char` type is `1` byte. Another important point to note is that when we increment and decrement pointer variable by adding or subtracting numbers then it is not necessary that the pointer variable still points to a valid memory location. So, we must always pay special attention when we move the pointer in this way. Generally, we use pointer arithmetic with arrays because elements of an array are arranged in contiguous memory locations, which is discussed in detail in the next chapter.

The following program shows pointer arithmetic.

Expected Output:

Try It now:

## Pointer Arithmetic between Two Pointers

If we have two pointers `p1` and `p2` of base type pointer to `int` with addresses `1000` and `1016` respectively, then `p2 - p1` will give `4`, since the size of `int` type is `4` bytes. If you subtract `p2` from `p1` i.e `p1 - p2` then answer will be negative i.e `-4`.

The following program demonstrates pointer arithmetic between two pointers of the same type.

Expected Output:

Try it now:

## Combining Indirection operator (*) and Increment/Decrement operator

While processing elements of an array (as you will see in the next chapter) C programmers often mix indirection operator (`*`) and increment/decrement operator( `++` and `--` ).

Always remember that the precedence of indirection operator (`*`) and increment/decrement operator are same and they associate from right to left (see Operator Precedence and Associativity in C).

Suppose `x` is integer variable and `p` is a pointer to `int`. Now consider the following statements and try to interpret them.

Example 1:

Since `*` and `++` operators have the same precedence and associate from right to left `++` will be applied to `p`, not to `*p`. Because the increment operator is postfix, so first the value of `p` is used in the expression then it will be incremented. Therefore, the first integer pointed by `p` will be dereferenced and assigned to `x`, then the value of `p` will be incremented by `1`.

Example 2:

Here `*` operator will be first applied to `p` then `++` will be applied to `*p`. Therefore first integer pointer is dereferenced, the value obtained from dereferencing is incremented and eventually assigned to `x`.

Example 3:

`++` operator is prefixed, so first, `p` will be incremented, then the value at the new address is dereferenced and assigned to `x`.

Note: If you still have any confusion, you can always use `()` around expression which you want to evaluate first.

## Pointer Comparison

You can use relational operators ( `<`, `<=`, `>`, `>=` , `==` , `!=`) with pointers. The `==` and `!=`operators are used to compare two pointers whether they contain the same address or not. Two pointers are equal when they both are null, or contains the address of the same variable. Use of these (i.e `==` and `!=`) operators are valid only when pointers are of the same base type, or between a null pointer and any other pointer, or void pointer(will be discussed later) and any other pointer. Use of other relational operators (`<`, `<=`, `>`, `>=`) to compare two pointers is meaningful only when they both point to the elements of the same array.

## Pointer to Pointer

As we know the pointer is a variable that contains the memory address. The pointer variable itself occupies some space in memory and hence it also has a memory address. We can store the address of the pointer variable in some other variable, which is known as pointer to pointer. The syntax of declaring a pointer to a pointer is as follows:

Syntax: `data_type **p;`

Let’s take an example:

Here `ip` is of type `(int *)` or pointer to `int`, `iip` is of type `(int **)` or pointer to pointer to `int`.

We know that `*ip` will give value at address `ip` i.e value of `i`. Can you guess what value will `**iip` will return?

We know that indirection operator evaluated from right to left so `**iip` can also be written as

`*iip` means value at address `iip` or address stored at `ip`. On dereferencing address stored at `ip` we will get the value stored in the variable `i`.

Therefore `**iip` gives the value stored in the variable `i`.

The following program demonstrates how to use pointer to pointer in int.

Expected Output:

### 9 thoughts on “Pointer Arithmetic in C”

1. Nice, easy, beneficial

2. Easy to understand , nice

3. a well-explained article.

4. Fantastic page to refresh pointers after many years of hacking with pointerless languages.

typo: &f should be &d
double d = 2.3, *dp = &f;

and in text “therefore ip, fp and cp are ” fp->dp

• Fixed!

5. Thank You for the information, It cleared so many doubts for me

6. You can use different colors for border in the pictures with data and with pointer for better understanding.

• Thanks for suggestion. I will update them soon.

7. Pointer size depends on the word size of the architecture, not on the size of data type it is pointing to. For example, on x64 machines, pointer size is always 8 bytes irrespective of the data type it is pointing to (be it char, int, float). This is because the word size on x64 is 64 bits i.e 8 bytes.