C Tutorial C References

C - Pointer Arithmetic



A pointer contains the address of a variable, which is a numeric value. C allows few arithmetic operations on a pointer which are slightly different from regular mathematical operations. It supports four arithmetic operators on a pointer which are: ++, --, +, -.

Increment/Decrement of a Pointer

Increment: When a pointer is incremented, it starts pointing to the immediate next location. This is somewhat different from regular mathematical operations since the value of the pointer gets increased by the size of the data type it is pointing to. For Example: let us consider that ptr is an integer pointer which points to the address 1000. When it is incremented, its value gets increased by 4 (size of 64-bit int) and it will point to new address 1004.

Decrement: Similarly, When a pointer is decremented, it starts pointing to the immediate previous location and the value of the pointer gets decreased by the size of the data type it is pointing to. For Example: let us consider that ptr is an integer pointer which points to the address 1000. When it is decremented, its value gets decreased by 4 (size of 64-bit int) and it will point to new address 996.

Example:

The example below illustrates on increment/decrement of a pointer.

#include <stdio.h>
 
int main (){
  int Var = 10;
  int *p1, *p2;

  p1 = p2 = &Var;

  printf("p1 before incrementing: %p\n", p1);
  //incrementing p1
  p1++;
  printf("p1 after incrementing: %p\n", p1);

  printf("\np2 before decrementing: %p\n", p2);
  //decrementing p2
  p2--;
  printf("p2 after decrementing: %p\n", p2);
  return 0;
}

The output of the above code will be:

p1 before incrementing: 0x7fff664ac824
p1 after incrementing: 0x7fff664ac828

p2 before decrementing: 0x7fff664ac824
p2 after decrementing: 0x7fff664ac820

Addition/Subtraction of an integer to a Pointer

Addition: When an integer n is added to a pointer, n is first multiplied by the data type the pointer is pointing to, then it is added to the pointer.

Subtraction: Similarly, When an integer n is subtracted from a pointer, n is first multiplied by the data type the pointer is pointing to, then it is subtracted from the pointer.

Example:

The example below illustrates on addition/subtraction of an integer to a pointer.

#include <stdio.h>
 
int main (){
  int Var = 10;
  int *p1, *p2;

  p1 = p2 = &Var;

  printf("p1 : %p\n", p1);
  //adding 5 to p1
  p1 = p1 + 5;
  printf("(p1 + 5) : %p\n", p1);

  printf("\np2 : %p\n", p2);
  //subtracting 5 from p2
  p2 = p2 - 5;
  printf("(p2 - 5) : %p\n", p2);
  return 0;
}

The output of the above code will be:

p1 : 0x7ffd913bce74
(p1 + 5) : 0x7ffd913bce88

p2 : 0x7ffd913bce74
(p2 - 5) : 0x7ffd913bce60

Pointer Arithmetic on Arrays

By using the above discussed operations, a pointer can be used to iterate over an array.

Example:

Consider the example below where pointer is used to iterate over an array.

#include <stdio.h>
 
int main (){
  int Arr[5] = {10, 20, 30, 40, 50};
  int *p1, *p2;

  p1 = Arr;      //pointing to Arr[0]
  p2 = Arr + 4;  //pointing to Arr[4]

  printf("Arr contains: ");
  //incrementing p1 to iterate in forward direction
  for(int i = 0; i < 5; i++) {
    printf("%i ", *p1);
    p1++;
  }

  printf("\nArr contains: ");
  //decrementing p2 to iterate in backward direction
  for(int i = 0; i < 5; i++) {
    printf("%i ", *p2);
    p2--;
  }
  return 0;
}

The output of the above code will be:

Arr contains: 10 20 30 40 50 
Arr contains: 50 40 30 20 10 

Subtracting two Pointers

Subtracting two pointers is a valid operation although adding two pointers is not a valid operation. The subtraction of two pointers is possible only when they have the same data type. The subtraction of two pointers returns the increments between the two pointers.

Example:

The example below illustrates the subtraction of two int type pointers.

#include <stdio.h>
 
int main (){
  int Var = 10;
  int *p1, *p2;

  p1 = &Var;
  p2 = p1 + 5;

  printf("(p2 - p1) : %lu", (p2-p1));
  return 0;
}

The output of the above code will be:

(p2 - p1) : 5

Pointer Comparison

Pointers can be compared using relational operators, such as ==, <, >, <= and >=. If two pointers are related to each other such as pointing to the elements of same array, then they can be compared meaningfully.

Example:

In the example below, pointer comparison property is used to iterate over an array. A pointer pointing to an element of an array is incremented in each loop until its value becomes greater than the address of the last element of the array.

#include <stdio.h>

int main (){
  int Arr[5] = {10, 20, 30, 40, 50};
  int *p1;

  p1 = Arr;   //pointing to Arr[0]
  
  while(p1 <= &Arr[4]) {
    printf("p1 : %p\n", p1);
    printf("*p1 : %i\n", *p1);
    p1++;
  }
  return 0;
}

The output of the above code will be:

p1 : 0x7ffde6d995d0
*p1 : 10
p1 : 0x7ffde6d995d4
*p1 : 20
p1 : 0x7ffde6d995d8
*p1 : 30
p1 : 0x7ffde6d995dc
*p1 : 40
p1 : 0x7ffde6d995e0
*p1 : 50

❮ C - Pointers