Pointer Arithmetics in C with Examples

Pointers variables are also known as address data types because they are used to store the address of another variable. The address is the memory location that is assigned to the variable. It doesn’t store any value.
Hence, there are only a few operations that are allowed to perform on Pointers in C language. The operations are slightly different from the ones that we generally use for mathematical calculations. The operations are:

  1. Increment/Decrement of a Pointer
  2. Addition of integer to a pointer
  3. Subtraction of integer to a pointer
  4. Subtracting two pointers of the same type

Increment/Decrement of a Pointer

Increment: It is a condition that also comes under addition. When a pointer is incremented, it actually increments by the number equal to the size of the data type for which it is a pointer.
For Example:
If an integer pointer that stores address 1000 is incremented, then it will increment by 2(size of an int) and the new address it will points to 1002. While if a float type pointer is incremented then it will increment by 4(size of a float) and the new address will be 1004.

Decrement: It is a condition that also comes under subtraction. When a pointer is decremented, it actually decrements by the number equal to the size of the data type for which it is a pointer.
For Example:
If an integer pointer that stores address 1000 is decremented, then it will decrement by 2(size of an int) and the new address it will points to 998. While if a float type pointer is decremented then it will decrement by 4(size of a float) and the new address will be 996.

Below is the program to illustrate pointer increment/decrement:



filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to illustrate
// pointer increment/decrement
  
#include <stdio.h>
  
// Driver Code
int main()
{
    // Integer variable
    int N = 4;
  
    // Pointer to an integer
    int *ptr1, *ptr2;
  
    // Pointer stores
    // the address of N
    ptr1 = &N;
    ptr2 = &N;
  
    printf("Pointer ptr1 "
           "before Increment: ");
    printf("%p \n", ptr1);
  
    // Incrementing pointer ptr1;
    ptr1++;
  
    printf("Pointer ptr1 after"
           " Increment: ");
    printf("%p \n\n", ptr1);
  
    printf("Pointer ptr1 before"
           " Decrement: ");
    printf("%p \n", ptr1);
  
    // Decrementing pointer ptr1;
    ptr1--;
  
    printf("Pointer ptr1 after"
           " Decrement: ");
    printf("%p \n\n", ptr1);
  
    return 0;
}

chevron_right


Output:

Pointer ptr1 before Increment: 0x7ffcb19385e4 
Pointer ptr1 after Increment: 0x7ffcb19385e8 

Pointer ptr1 before Decrement: 0x7ffcb19385e8 
Pointer ptr1 after Decrement: 0x7ffcb19385e4

Addition

When a pointer is added with a value, the value is first multiplied by the size of data type and then added to the pointer.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to illustrate pointer Addition
#include <stdio.h>
  
// Driver Code
int main()
{
    // Integer variable
    int N = 4;
  
    // Pointer to an integer
    int *ptr1, *ptr2;
  
    // Pointer stores the address of N
    ptr1 = &N;
    ptr2 = &N;
  
    printf("Pointer ptr2 before Addition: ");
    printf("%p \n", ptr2);
  
    // Addition of 3 to ptr2
    ptr2 = ptr2 + 3;
    printf("Pointer ptr2 after Addition: ");
    printf("%p \n", ptr2);
  
    return 0;
}

chevron_right


Output:

Pointer ptr2 before Addition: 0x7fffffdcd984 
Pointer ptr2 after Addition: 0x7fffffdcd990

Subtraction

When a pointer is subtracted with a value, the value is first multiplied by the size of the data type and then subtracted from the pointer.

Below is the program to illustrate pointer Subtraction:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to illustrate pointer Subtraction
#include <stdio.h>
  
// Driver Code
int main()
{
    // Integer variable
    int N = 4;
  
    // Pointer to an integer
    int *ptr1, *ptr2;
  
    // Pointer stores the address of N
    ptr1 = &N;
    ptr2 = &N;
  
    printf("Pointer ptr2 before Subtraction: ");
    printf("%p \n", ptr2);
  
    // Subtraction of 3 to ptr2
    ptr2 = ptr2 - 3;
    printf("Pointer ptr2 after Subtraction: ");
    printf("%p \n", ptr2);
  
    return 0;
}

chevron_right


Output:



Pointer ptr2 before Subtraction: 0x7ffcf1221b24 
Pointer ptr2 after Subtraction: 0x7ffcf1221b18

Subtraction of Two Pointers

The subtraction of two pointers is possible only when they have the same data type. The result is generated by calculating the difference between the addresses of the two pointers and calculating how many bits of data it is according to the pointer data type. The subtraction of two pointers gives the increments between the two pointers.
For Example:
Two integer pointers say ptr1(address:1000) and ptr2(address:1016) are subtracted. The difference between address is 16 bytes. Since the size of int is 2 bytes, therefore the increment between ptr1 and ptr2 is given by (16/2) = 8.

Below is the implementation to illustrate the Subtraction of Two Pointers:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to illustrate Subtraction
// of two pointers
#include <stdio.h>
  
// Driver Code
int main()
{
    int x;
  
    // Integer variable
    int N = 4;
  
    // Pointer to an integer
    int *ptr1, *ptr2;
  
    // Pointer stores the address of N
    ptr1 = &N;
    ptr2 = &N;
  
    // Incrementing ptr2 by 3
    ptr2 = ptr2 + 3;
  
    // Subtraction of ptr2 and ptr1
    x = ptr2 - ptr1;
  
    // Print x to get the Increment
    // between ptr1 and ptr2
    printf("Subtraction of ptr1 "
           "& ptr2 is %d\n",
           x);
  
    return 0;
}

chevron_right


Output:

Subtraction of ptr1 & ptr2 is 3

Pointer Arthemetic on Arrays:
Pointers contain addresses. Adding two addresses makes no sense because there is no idea what it would point to. Subtracting two addresses lets you compute the offset between the two addresses. An array name acts like a pointer constant. The value of this pointer constant is the address of the first element. For Example: if an array named arr then arr and &arr[0] can be used to reference array as a pointer.

Below is the program to illustrate the Pointer Arithmetic on arrays:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to illustrate the array
// traversal using pointers
#include <stdio.h>
  
// Driver Code
int main()
{
  
    int N = 5;
  
    // An array
    int arr[] = { 1, 2, 3, 4, 5 };
  
    // Declare pointer variable
    int* ptr;
  
    // Point the pointer to first
    // element in array arr[]
    ptr = arr;
  
    // Traverse array using ptr
    for (int i = 0; i < N; i++) {
  
        // Print element at which
        // ptr points
        printf("%d ", ptr[0]);
        ptr++;
    }
}

chevron_right


Output:

1 2 3 4 5

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to illustrate the array
// traversal using pointers in 2D array
#include <stdio.h>
  
// Function to traverse 2D array
// using pointers
void traverseArr(int* arr,
                 int N, int M)
{
  
    int i, j;
  
    // Travere rows of 2D matrix
    for (i = 0; i < N; i++) {
  
        // Travere columns of 2D matrix
        for (j = 0; j < M; j++) {
  
            // Print the element
            printf("%d ", *((arr + i * M) + j));
        }
        printf("\n");
    }
}
  
// Driver Code
int main()
{
  
    int N = 3, M = 2;
  
    // A 2D array
    int arr[][2] = { { 1, 2 },
                     { 3, 4 },
                     { 5, 6 } };
  
    // Function Call
    traverseArr((int*)arr, N, M);
    return 0;
}

chevron_right


Output:

1 2 
3 4 
5 6

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.