OverIQ.com

Pointer Arithmetic in C

Last updated on September 24, 2020


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.

1
2
3
int i = 12, *ip = &i;
double d = 2.3, *dp = &d;
char ch = 'a', *cp = &ch;

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<stdio.h>

int main()
{
    int i = 12, *ip = &i;
    double d = 2.3, *dp = &d;
    char ch = 'a', *cp = &ch;

    printf("Value of ip = %u\n", ip);
    printf("Value of dp = %u\n", dp);
    printf("Value of cp = %u\n\n", cp);

    printf("Value of ip + 1 = %u\n", ip + 1);
    printf("Value of dp + 1 = %u\n", dp + 1);
    printf("Value of cp + 1 = %u\n\n", cp + 1);

    printf("Value of ip + 2 = %u\n", ip + 2);
    printf("Value of dp + 2 = %u\n", dp + 2);
    printf("Value of cp + 2 = %u\n", cp + 2);

    return 0;
}

Try it now

Expected Output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
Value of ip = 2293316
Value of dp = 2293304
Value of cp = 2293303

Value of ip + 1 = 2293320
Value of dp + 1 = 2293312
Value of cp + 1 = 2293304

Value of ip + 2 = 2293324
Value of dp + 2 = 2293320
Value of cp + 2 = 2293305

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 the answer will be negative i.e -4.

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include<stdio.h>

int main()
{
    int i1 = 12, *ip1 = &i1;
    int i2 = 12, *ip2 = &i2;

    printf("Value of ip1 or address of i1 = %u\n", ip1);
    printf("Value of ip2 or address of i2 = %u\n\n", ip2);

    printf("ip2 - ip1 = %d\n", ip1 - ip2);
    printf("ip1 - ip2 = %d\n", ip2 - ip1);

    // signal to operating system program ran fine
    return 0;
}

Try it now

Expected Output:

1
2
3
4
5
Value of ip1 or address of i1 = 2686788
Value of ip2 or address of i2 = 2686780

ip2 - ip1 = 2
ip1 - ip2 = -2

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:

x = *p++;

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:

x = ++*p;

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:

x = *++p;

The ++ 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:

1
2
3
int i = 10;
int *ip = &i;
int **iip = &ip;

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?

**iip

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

*(*iip)

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

1
2
3
*(*iip)
=> *ip
=> i

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

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>

int main()
{
    int i = 10;
    int *ip = &i;
    int **iip = &ip;

    printf("Value of i = %d\n\n", i);

    printf("Address of i = %u\n", &i);
    printf("Value of ip = %d\n\n", ip);

    printf("Address of ip = %u\n", &ip);
    printf("Value of iip = %d\n\n", iip);

    printf("Value of *iip = value of ip = %d\n", *iip);
    printf("Value of **iip = value of i = %d\n\n", **iip);

    return 0;
}

Try it now

Expected Output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Value of i = 10

Address of i = 2293332
Value of ip = 2293332

Address of ip = 2293320
Value of iip = 2293320

Value of *iip = value of ip = 2293332
Value of **iip = value of i = 10