Operation on Pointers in C | Pointer Arithmetic in C

In C language a pointer is nothing but an address which holds a numeric value. So that we can perform arithmetic operations on the pointer variables.

There are five arithmetic operations, which can be used with pointer variables :

  • Increment (++)
  • Decrement (--)
  • Addition ( + )
  • Subtraction ( - )
  • Comparision

Increment and Decrement Operation :

The increment/Decrement operator is used on the pointer which points to an array, because as we know that an array is an contiguous memory location. So by incrementing/decrementing the address pointed by a pointer, we can access the elements of an array back and forth.

To understand the pointer arithmetic, Lets consider an integer pointer ptr, which points to an array, and currently pointer holds the address 3000. And also we assume that the size of an integer is 4 bytes.


The below expression will increment the pointer variable :
 ptr++;
Now the pointer ptr points to 3004.


Also note that when we increment the pointer  ptr then it points to 3004, not 3001. The reason for this is that each time ptr is incremented, it will point to the next integer, and  the size of integer is 4 bytes. And if ptr is a character pointer then it will increment only 1 byte ( 3001). For example suppose chp is a character pointer and intp is an integer pointers which points to the address 3000 then
 char *chp = (char*)3000; 
 int *intp = (int*)3000;

Now lets take a look at the below C examples :
#include <stdio.h>
#include <stdlib.h>

int main() {
  int arr[] = {10, 20, 30, 40, 50};
  int *ptr;
  ptr = arr;
  printf("1st Element (Address : 0x%x) : %d\n", ptr, *ptr);
  ptr++;  
  printf("2nd Element (Address : 0x%x) : %d\n", ptr, *ptr);
  ptr++;
  printf("3rd Element (Address : 0x%x) : %d\n", ptr, *ptr);
  ptr++;
  printf("4th Element (Address : 0x%x) : %d\n", ptr, *ptr);
  ptr++;
  printf("5th Element (Address : 0x%x) : %d\n", ptr, *ptr);
  return 0;
}
Output :

1st Element (Address : 0x74541b30) : 10
2nd Element (Address : 0x74541b34) : 20
3rd Element (Address : 0x74541b38) : 30
4th Element (Address : 0x74541b3c) : 40
5th Element (Address : 0x74541b40) : 50

At the output we ca see that the address hold by pointer is increments 4 bytes within each ++ operation.

Note : the above output is generated on 64-bit system with gcc compiler.

Similarly we can also use the decrement operator within the pointer. For example :
#include <stdio.h>
#include <stdlib.h>

int main() {
  int arr[] = {10, 20, 30, 40, 50};
  int *ptr;
  ptr = &arr[4];
  printf("5th Element (Address : 0x%x) : %d\n", ptr, *ptr);
  ptr--;  
  printf("4th Element (Address : 0x%x) : %d\n", ptr, *ptr);
  ptr--;
  printf("3rd Element (Address : 0x%x) : %d\n", ptr, *ptr);
  ptr--;
  printf("2nd Element (Address : 0x%x) : %d\n", ptr, *ptr);
  ptr--;
  printf("1st Element (Address : 0x%x) : %d\n", ptr, *ptr);
  return 0;
}
Output :

5th Element (Address : 0x304f1dd0) : 50
4th Element (Address : 0x304f1dcc) : 40
3rd Element (Address : 0x304f1dc8) : 30
2nd Element (Address : 0x304f1dc4) : 20
1st Element (Address : 0x304f1dc0) : 10

We can also add and subtract integer number with pointer variables. The expressions are :
ptr = ptr+3;
The above expression makes ptr points to the third element of an array. Now lets see an example C program :
#include <stdio.h>
#include <stdlib.h>

int main() {
  int arr[] = {10, 20, 30, 40, 50};
  int *ptr;
  ptr = arr;

  printf("1st Element (Address : 0x%x) : %d\n", ptr, *ptr);
  ptr = ptr + 2;
  printf("3rd Element (Address : 0x%x) : %d\n", ptr, *ptr);
  ptr = ptr + 2;
  printf("5th Element (Address : 0x%x) : %d\n", ptr, *ptr);
  ptr = ptr - 1;
  printf("4th Element (Address : 0x%x) : %d\n", ptr, *ptr);
  ptr = ptr - 2;
  printf("2nd Element (Address : 0x%x) : %d\n", ptr, *ptr);
  return 0;
}
Output :

1st Element (Address : 0xc30e36c0) : 10
3rd Element (Address : 0xc30e36c8) : 30
5th Element (Address : 0xc30e36d0) : 50
4th Element (Address : 0xc30e36cc) : 40
2nd Element (Address : 0xc30e36c4) : 20

Pointer Comparisons :

In C language we can compare two pointers by using rational operators, such as ==, <, and >. If the two pointers ptr1 and ptr2 point to variables that are related to each other, such as elements of the same array, then ptr1 and ptr2 can be meaningfully compared. For example suppose we have two pointers ptr1 and ptr2 which points to an array, then the below expression is perfectly valid :
if(ptr1 < ptr2) {
  printf("ptr1 points lower memory address then ptr2\n.");
}
Generally, pointer comparisions are used when two or more pointers point to a common object, such as an array. Now lets see an example C program.
#include <stdio.h>
#include <stdlib.h>

#define SIZE 5

int main() {
  int num[SIZE] = {10, 20, 30, 40, 50};
  int *ptr;
  int c=0;
  ptr = num;

  while(ptr <= &num[SIZE-1]) {
    printf("Address of num[%d] = %x\n", c, ptr);
    printf("Value of num[%d] = %d\n\n", c, *ptr);

    ptr++;
    c++;
  }

  return 0;
}
Output :

Address of num[0] = 0xb290f5c0
Value of num[0] = 10

Address of num[1] = 0xb290f5c4
Value of num[1] = 20

Address of num[2] = 0xb290f5c8
Value of num[2] = 30

Address of num[3] = 0xb290f5cc
Value of num[3] = 40

Address of num[4] = 0xb290f5d0
Value of num[4] = 50


Next Topic :