Pointer Arithmetics in C with Examples

Last Updated : 11 Jul, 2026

Pointer arithmetic refers to the valid arithmetic operations that can be performed on pointers in C. Since pointers store memory addresses instead of actual values, only a limited set of arithmetic operations is allowed.

  • Pointer arithmetic includes operations such as increment/decrement, adding or subtracting an integer, subtracting two pointers, and comparing pointers.
  • These operations move the pointer based on the size of the data type it points to, not by a single byte.
  • The memory addresses of pointers may change on each program execution due to ASLR (Address Space Layout Randomization) used by modern operating systems.

How Pointer Arithmetic Works

Unlike normal integers, pointer arithmetic does not increase or decrease the address by 1 byte. Instead, the compiler automatically multiplies the value by the size of the data type.

Formula:

New Address = Current Address ± (Number × Size of Data Type)

For example: Suppose an integer pointer stores the address 1000 and:

sizeof(int) = 4

Then:

ExpressionNew Address
ptr + 11004
ptr + 21008
ptr - 1996
ptr - 2992

Types of Pointer Arithmetic

Increment/Decrement of a Pointer

Increment:When a pointer is incremented, its address increases by the size of the data type it points to, not by 1 byte.

Example: If an int pointer stores address 1000, incrementing it moves it to 1004 (assuming sizeof(int) = 4).

Decrement:When a pointer is decremented, its address decreases by the size of the data type it points to.

Example: If an int pointer stores address 1000, decrementing it moves it to 996 (assuming sizeof(int) = 4).

pointer increment and decrement
 

It is assumed here that the architecture is 64-bit and all the data types are sized accordingly. For example, integer is of 4 bytes.

C
#include <stdio.h>
// pointer increment and decrement
// pointers are incremented and decremented 
// by the size of the data type they point to 
int main(){
    int a = 22;
    int *p = &a;
    
    // p = 6422288
    printf("p = %u\n", p); 
    p++;
    // p++ = 6422292    +4 
    // 4 bytes
    printf("p++ = %u\n", p); 
    p--;
    
    // p-- = 6422288     -4    
    // restored to original value
    printf("p-- = %u\n", p); 
    float b = 22.22;
    float *q = &b;
    
    // q = 6422284
    printf("q = %u\n", q);  
    q++;
    
    // q++ = 6422288      +4   
    // 4 bytes
    printf("q++ = %u\n", q); 
    q--;
    
    // q-- = 6422284       -4  
    // restored to original value
    printf("q-- = %u\n", q); 

    char c = 'a';
    char *r = &c;
    
    // r = 6422283
    printf("r = %u\n", r);   
    r++;
    
    // r++ = 6422284     +1   
    // 1 byte
    printf("r++ = %u\n", r);   
    r--;
    
    //r-- = 6422283     -1  
    // restored to original value
    printf("r-- = %u\n", r);   

    return 0;
}

Output
p = 1441900792
p++ = 1441900796
p-- = 1441900792
q = 1441900796
q++ = 1441900800
q-- = 1441900796
r = 1441900791
r++ = 1441900792
r-- = 1441900791

Note: Pointers can be output using %p, since most computers store address values in hexadecimal form, and using %p displays them in that format. However, for simplicity and understanding, we can also use %u to get the value in unsigned-integer form.

Addition of Integer to Pointer

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

For Example: Consider the same example as above where the ptr is an integer pointer that stores 1000 as an address. If we add integer 5 to it using the expression, ptr = ptr + 5, then, the final address stored in the ptr will be ptr = 1000 + sizeof(int) * 5 = 1020.

pointer addition

Example: Addition of Integer to Pointer

C
#include <stdio.h>

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;
}

Output
Pointer ptr2 before Addition: 0x7ffca373da9c 
Pointer ptr2 after Addition: 0x7ffca373daa8 

Subtraction  of Integer to Pointer

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

Example: If an int pointer stores the address 1000, subtracting 5 from it (ptr = ptr - 5) decreases the address by 5 × sizeof(int). Assuming sizeof(int) = 4, the new address becomes 980.

pointer substraction
C
#include <stdio.h>

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;
}

Output
Pointer ptr2 before Subtraction: 0x7ffd718ffebc 
Pointer ptr2 after Subtraction: 0x7ffd718ffeb0 

Subtraction of Two Pointers

Two pointers can be subtracted only if they point to elements of the same data type. The result represents the number of elements between the two memory locations, not the difference in bytes.

Example: If ptr1 stores address 1000 and ptr2 stores address 1004, the address difference is 4 bytes. Since sizeof(int) = 4, the result of ptr2 - ptr1 is 1.

C
#include <stdio.h>

int main(){
    int x = 6; 
    int N = 4;

    // Pointer declaration
    int *ptr1, *ptr2;
    
    // stores address of N
    ptr1 = &N; 
    
    // stores address of x
    ptr2 = &x; 

    printf(" ptr1 = %u, ptr2 = %u\n", ptr1, ptr2);
    // %p gives an hexa-decimal value,
    // We convert it into an 
    // unsigned int value by using %u

    // Subtraction of ptr2 and ptr1
    x = ptr1 - ptr2;

    // Print x to get the Increment
    // between ptr1 and ptr2
    printf("Subtraction of ptr1 "
           "& ptr2 is %d\n",
           x);

    return 0;
}

Output
 ptr1 = 2715594428, ptr2 = 2715594424
Subtraction of ptr1 & ptr2 is 1

Comparison of Pointers

We can compare the two pointers by using the comparison operators in C. We can implement this by using all operators in C >, >=, <, <=, ==, !=.  It returns true for the valid condition and returns false for the unsatisfied condition. 

  1. Step 1: Initialize the integer values and point these integer values to the pointer.
  2. Step 2: Now, check the condition by using comparison or relational operators on pointer variables.
  3. Step 3: Display the output.
C
#include <stdio.h>

int main(){
    // declaring array
    int arr[5];

    // declaring pointer to array name
    int* ptr1 = &arr;
    // declaring pointer to first element
    int* ptr2 = &arr[0];

    if (ptr1 == ptr2) {
        printf("Pointer to Array Name and First Element "
               "are Equal.");
    }
    else {
        printf("Pointer to Array Name and First Element "
               "are not Equal.");
    }

    return 0;
}

Output
Pointer to Array Name and First Element are Equal.

Comparison to NULL

A pointer can be compared or assigned a NULL value irrespective of what is the pointer type. Such pointers are called NULL pointers and are used in various pointer-related error-handling methods.

C
#include <stdio.h>

int main(){

    int* ptr = NULL;

    if (ptr == NULL) {
        printf("The pointer is NULL");
    }
    else {
        printf("The pointer is not NULL");
    }
    return 0;
}

Output
The pointer is NULL

Comparison operators on Pointers using an array

In the below approach, it results in the count of odd numbers and even numbers in an array. We are going to implement this by using a pointer.

  1. First, declare the length of an array and array elements.
  2. Declare the pointer variable and point it to the first element of an array.
  3. Initialize the count_even and count_odd. Iterate the for loop and check the conditions for the number of odd elements and even elements in an array
  4. Increment the pointer location ptr++ to the next element in an array for further iteration.
  5. Print the result.
C
#include <stdio.h>

int main(){
    int n = 10; 

    int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    
    // Declaration of pointer variable
    int* ptr; 

    // Pointer points the first (0th index)
    // element in an array
    ptr = arr; 
    int count_even = 0;
    int count_odd = 0;

    for (int i = 0; i < n; i++) {

        if (*ptr % 2 == 0) {
            count_even++;
        }
        if (*ptr % 2 != 0) {
            count_odd++;
        }
        
        // Pointing to the next 
        // element in an array
        ptr++; 
    }
    printf("No of even elements in an array is : %d",
           count_even);
    printf("\nNo of odd elements in an array is : %d",
           count_odd);
}

Output
No of even elements in an array is : 5
No of odd elements in an array is : 5

Pointer Arithmetic on Arrays

Pointers store memory addresses, so arithmetic on pointers follows specific rules. While some pointer operations are valid, adding two pointers is not allowed because it does not produce a meaningful memory address.

  • Subtracting two pointers of the same type returns the number of elements between their memory locations.
  • An array name acts as a constant pointer to its first element, allowing pointer arithmetic to traverse the array.

Example: if an array is named arr then arr and &arr[0] can be used to reference the array as a pointer.

Program 1 

C
#include <stdio.h>

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++;
    }
}

Output
1 2 3 4 5 
Comment