C programming pointer arithmetic – Pointer Arithmetic in C Programming

C programming pointer arithmetic: We can perform arithmetic operations on pointer variable just as you can a numeric value. As we know that, a pointer in C is a variable which is used to store the memory address which is a numeric value. The arithmetic operations on pointer variable effects the memory address pointed by pointer.

Valid Pointer Arithmetic Operations

  • Adding a number to pointer.
  • Subtracting a number form a pointer.
  • Incrementing a pointer.
  • Decrementing a pointer.
  • Subtracting two pointers.
  • Comparison on two pointers.

Invalid Pointer Arithmetic Operations

  • Addition of two pointers.
  • Division of two pointers.
  • Multiplication of two pointers.

Incrementing a Pointer

Incrementing pointers in c: Let ptr be an integer pointer which points to the memory location 5000 and size of an integer variable is 32-bit(4 bytes). Now, when we increment pointer ptr

    ptr++;

it will point to memory location 5004 because it will jump to the next integer location which is 4 bytes next to the current location. Incrementing a pointer is not same as incrementing an integer value. On incrementing, a pointer will point to the memory location after skipping N bytes, where N is the size of the data type(in this case it is 4).

ptr++ is equivalent to ptr + (sizeof(pointer_data_type)). 

“Incrementing a pointer increases its value by the number of bytes of its data type”

A character(1 bytes) pointer on increment jumps 1 bytes.
An integer(4 bytes) pointer on increment jumps 4 bytes.

This is a very important feature of pointer arithmetic operations which we will use in array traversal using pointers.

Decrementing a Pointer

Increment pointer in c: Similarly, Decrementing a pointer will decrease its value by the number of bytes of its data type. Hence, after

    ptr--;

ptr will point to 4996.

ptr--; is equivalent to ptr - (sizeof(pointer_data_type)).

Adding Numbers to Pointers

Adding a number N to a pointer leads the pointer to a new location after skipping N times size of data type.

ptr + N =  ptr + (N * sizeof(pointer_data_ype))

For example, Let ptr be a 4-byte integer pointer, initially pointing to location 5000.
Then ptr + 5 = 5000 + 4*5 = 5020. Pointer ptr will now point at memory address 5020.

Subtracting Numbers from Pointers

Subtracting a number N from a pointers is similar to adding a number except in Subtraction the new location will be before current location by N times size of data type.

ptr - N =  ptr - (N * sizeof(pointer_data_ype))

For example, Let ptr be a 6-byte double pointer, initially pointing to location 5000.
Then ptr – 3 = 5000 – 6*3 = 4982. Pointer ptr will now point at memory address 4982.

Subtracting Pointers

The difference between two pointer returns indicates “How apart the two Pointers are. It gives the total number of elements between two pointers.

For example, Let size of integer is 4 bytes. If an integer pointer ‘ptr1’ points at memory location 10000 and integer pointer ‘ptr’ points at memory location 10008, the result of ptr2 – ptr1 is 2.

C program to show pointer arithmetic

Pointer Arithmetic in C Programming

#include<stdio.h>
#include<conio.h>
 
void main() {
     int int_var = 10, *int_ptr;
     char char_var = 'A', *char_ptr;
  float float_val = 4.65, *float_ptr; 
      
     /* Initialize pointers */
     int_ptr = &int_var;
     char_ptr = &char_var;
     float_ptr = &float_val;
      
     printf("Address of int_var = %u\n", int_ptr);
     printf("Address of char_var = %u\n", char_ptr);
     printf("Address of float_var = %u\n\n", float_ptr);
      
     /* Incrementing pointers */
     int_ptr++;
     char_ptr++;
     float_ptr++;
     printf("After increment address in int_ptr = %u\n", int_ptr);
     printf("After increment address in char_ptr = %u\n", char_ptr);
     printf("After increment address in float_ptr = %u\n\n", float_ptr);
      
     /* Adding 2 to pointers */
     int_ptr = int_ptr + 2;
     char_ptr = char_ptr + 2;
     float_ptr = float_ptr + 2;
      
     printf("After addition address in int_ptr = %u\n", int_ptr);
     printf("After addition address in char_ptr = %u\n", char_ptr);
     printf("After addition address in float_ptr = %u\n\n", float_ptr);
      
     getch();
     return 0;
}

Output

Address of int_var = 2293300
Address of char_var = 2293299
Address of float_var = 2293292

After increment address in int_ptr = 2293304
After increment address in char_ptr = 2293300
After increment address in float_ptr = 2293296

After addition address in int_ptr = 2293312
After addition address in char_ptr = 2293302
After addition address in float_ptr = 2293304