• C Programming Video Tutorials

Pointer Arithmetics in C



A pointer variable in C stores the address of another variable. The address is always an integer. So, can we perform arithmetic operations such as addition and subtraction on the pointers? In this chapter, we will explain which arithmetic operators use pointers in C as operands, and which operations are not defined to be performed with pointers.

C pointers arithmetic operations are different from the general arithmetic operations. The following are some of the important pointer arithmetic operations in C:

  • Increment and Decrement of a Pointer
  • Addition and Subtraction of Integer to Pointer
  • Subtraction of Pointers
  • Comparison of Pointers

Let us discuss all these pointer arithmetic operations in detail with the help of examples.

Increment and Decrement of a Pointer

We know that "++" and "--" are used as the increment and decrement operators in C. They are unary operators, used in prefix or postfix manner with numeric variable operands, and they increment or decrement the value of the variable by one.

Assume that an integer variable "x" is created at address 1000 in the memory, with 10 as its value. Then, "x++" makes the value of "x" as 11.

int x = 10;   // created at address 1000

x++;          // x becomes 11

What happens if we declare "y" as pointer to "x" and increment "y" by 1 (with "y++")? Assume that the address of "y" itself is 2000.

int x = 10;   // created at address 1000

// "y" is created at address 2000 
// it holds 1000 (address of "x")
int *y = &x ;

y++;          // y becomes 1004

Since the variable "y" stores 1000 (the address of "x"), we expect it to become 1001 because of the "++" operator, but it increments by 4, which is the size of "int" variable.

The is why because, if the address of "x" is 1000, then it occupies 4 bytes: 1000, 1001, 1002 and 1003. Hence, the next integer can be put only in 1004 and not before it. Hence "y" (the pointer to "x") becomes 1004 when incremented.

Example of Incrementing a Pointer

The following example shows how you can increment a pointer −

#include <stdio.h>

int main(){

   int x = 10;
   int *y = &x;

   printf("Value of y before increment: %d\n", y);

   y++;

   printf("Value of y after increment: %d", y);
}

Output

Run the code and check its output −

Value of y before increment: 6422036
Value of y after increment: 6422040

You can see that the value has increased by 4. Similarly, the "--" operator decrements the value by the size of the data type.

Example of Decrementing a Pointer

Let us change the types of "x" and "y" to "double" and "float" and see the effect of decrement operator.

#include <stdio.h>

int main(){

   double x = 10;
   double *y = &x;
   
   printf("value of y before decrement: %ld\n", y);
   
   y--;
   
   printf("value of y after decrement: %ld", y);
}

Output

Value of y before decrement: 6422032
Value of y after decrement: 6422024

When an array is declared, the elements are stored in adjacent memory locations. In case of "int" array, each array subscript is placed apart by 4 bytes, as the following figure shows −

Memory Locations

Hence, if a variable stores the address of 0th element of the array, then the "increment" takes it to the 1st element.

Example of Traversing an Array by Incrementing Pointer

The following example shows how you can traverse an array by incrementing a pointer successively −

#include <stdio.h>

int main(){

   int a[]= {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
   int len = sizeof(a)/sizeof(int);
   int *x = a;
   int i = 0;

   for(i = 0; i < len; i++){
      printf("Address of subscript %d = %d Value = %d\n", i, x, *x);
      x++;
   }

   return 0;
}

Output

Run the code and check its output −

Address of subscript 0 = 6421984 Value = 10
Address of subscript 1 = 6421988 Value = 20
Address of subscript 2 = 6421992 Value = 30
Address of subscript 3 = 6421996 Value = 40
Address of subscript 4 = 6422000 Value = 50
Address of subscript 5 = 6422004 Value = 60
Address of subscript 6 = 6422008 Value = 70
Address of subscript 7 = 6422012 Value = 80
Address of subscript 8 = 6422016 Value = 90
Address of subscript 9 = 6422020 Value = 100

Addition and Subtraction of Integer to Pointer

An integer value can be added and subtracted to a pointer. When an integer is added to a pointer, the pointer points to the next memory address. Similarly, when an integer is subtracted from a pointer, the pointer points to the previous memory location.

Addition and subtraction of an integer to a pointer does not add and subtract that value to the pointer, multiplication with the size of the data type is added or subtracted to the pointer.

For example, there is an integer pointer variable ptr and it is pointing to an address 123400, if you add 1 to the ptr (ptr+1), it will point to the address 123404 (size of an integer is 4).

Let's evaluate it,

ptr = 123400 
ptr = ptr + 1
ptr = ptr + sizeof(int)*1
ptr = 123400 + 4
ptr = 123404

Example of Adding Value to a Pointer

In the following example, we are declaring an array and pointer to an array. Initializing the pointer with the first element of the array and then adding an integer value (2) to the pointer to get the third element of the array.

#include <stdio.h>

int main() {
  int int_arr[] = {12, 23, 45, 67, 89};
  int *ptrArr = int_arr;

  printf("Value at ptrArr: %d\n", *ptrArr);

  // Adding 2 in ptrArr
  ptrArr = ptrArr + 2;

  printf("Value at ptrArr after adding 2: %d\n", *ptrArr);

  return 0;
}

Output

Value at ptrArr: 12
Value at ptrArr after adding 2: 45

Example of Subtracting Value to a Pointer

In the following example, we are declaring an array and pointer to an array. Initializing the pointer with the last element of the array and then subtracting an integer value (2) from the pointer to get the third element of the array.

#include <stdio.h>

int main() {
  int int_arr[] = {12, 23, 45, 67, 89};
  int *ptrArr = &int_arr[4]; // points to last element

  printf("Value at ptrArr: %d\n", *ptrArr);

  // Subtracting 2 in ptrArr
  ptrArr = ptrArr - 2;

  printf("Value at ptrArr after adding 2: %d\n", *ptrArr);

  return 0;
}

Output

Value at ptrArr: 89
Value at ptrArr after adding 2: 45

Subtraction of Pointers

We are familiar with the "+" and "−" operators when they are used with regular numeric operands. However, when you use these operators with pointers, they behave in a little different way.

Since pointers are fairly large integers (especially in modern 64-bit systems), addition of two pointers is meaningless. When we add a 1 to a pointer, it points to the next location where an integer may be stored. Obviously, when we add a pointer (itself a large integer), the location it points may not be in the memory layout.

However, subtraction of two pointers is realistic. It returns the number of data types that can fit in the two pointers.

Example of Subtracting Two Pointers

Let us take the array in the previous example and perform the subtraction of pointers of a[0] and a[9]

#include <stdio.h>

int main(){

   int a[]= {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
   int *x = &a[0]; // zeroth element
   int *y = &a[9]; // last element

   printf("Add of a[0]: %ld add of a[9]: %ld\n", x, y);
   printf("Subtraction of two pointers: %ld", y-x);

}

Output

Run the code and check its output −

Add of a[0]: 140729162482768 add of a[9]: 140729162482804
Subtraction of two pointers: 9

It can be seen that the numerical difference between the two integers is 36; it suggests that the subtraction is 9, because it can accommodate 9 integers between the two pointers.

Comparison of Pointers

Pointers may be compared by using relational operators such as "==", "<", and ">". If "p1" and "p2" point to variables that are related to each other (such as elements of the same array), then "p1" and "p2" can be meaningfully compared.

Example of Comparing Pointers

In the following example, we are declaring two pointers and initializing them with the first and last elements of the array respectively. We will keep incrementing the first variable pointer as long as the address to which it points is either less than or equal to the address of the last element of the array, which is "&var[MAX − 1]" (i.e., the second pointer).

#include <stdio.h>

const int MAX = 3;

int main() {
  int var[] = {10, 100, 200};
  int i, *ptr1, *ptr2;

  // Initializing pointers
  ptr1 = var;
  ptr2 = &var[MAX - 1];

  while (ptr1 <= ptr2) {
    printf("Address of var[%d] = %p\n", i, ptr1);
    printf("Value of var[%d] = %d\n", i, *ptr1);

    /* point to the previous location */
    ptr1++;
    i++;
  }

  return 0;
}

Output

Run the code and check its output −

Address of var[0] = 0x7ffe7101498c
Value of var[0] = 10
Address of var[1] = 0x7ffe71014990
Value of var[1] = 100
Address of var[2] = 0x7ffe71014994
Value of var[2] = 200
Advertisements