Pointer Arithmetic in C

Pointer Arithmetic refers to performing mathematical operations on pointers. Since pointers represent memory addresses, increasing or decreasing them moves the pointer by the size of its data type.

Why Pointer Arithmetic?

Pointer arithmetic allows you to:

Valid Pointer Arithmetic Operations

You can perform:

You CANNOT perform:

How Increment Works?

When a pointer increments, it does not increase by 1 byte, it increases by the size of its data type.

Example: Pointer Increment

#include <stdio.h>

int main() {

    int a = 10;
    int *p = &a;

    printf("%p\n", p);
    p++;
    printf("%p\n", p);

    return 0;
}

This moves the pointer ahead by 4 bytes (size of int).

Pointer and Arrays

#include <stdio.h>

int main() {

    int a[3] = {10, 20, 30};
    int *p = a;

    printf("%d\n", *p);
    p++;
    printf("%d\n", *p);

    return 0;
}

Pointer moves across array indexes: a[0] → a[1] → a[2]

Pointer Subtraction

Two pointers can be subtracted only if they belong to the same array.

int a[5] = {1,2,3,4,5};
int *p = &a[1];
int *q = &a[4];

printf("%ld", q - p);   // Output: 3

Pointer Comparison

while(p < q){
    p++;
}

Pointer Arithmetic Rules

Real-Life Uses

Pointer Arithmetic – 10 Examples

1. Incrementing a Pointer

#include <stdio.h>

int main() {

    int a = 10;
    int *p = &a;

    printf("Address before: %p\n", p);
    p++;
    printf("Address after: %p", p);

    return 0;
}

2. Pointer Increment on Array

#include <stdio.h>

int main() {

    int arr[] = {10,20,30};
    int *p = arr;

    printf("%d\n", *p);
    p++;  
    printf("%d\n", *p);

    return 0;
}

3. Decrementing a Pointer

#include <stdio.h>

int main() {

    int arr[] = {5,10,15};
    int *p = &arr[2];

    printf("%d\n", *p);
    p--;
    printf("%d", *p);

    return 0;
}

4. Adding an Integer to Pointer

#include <stdio.h>

int main() {

    int arr[] = {2,4,6,8,10};
    int *p = arr;

    p = p + 3;

    printf("%d", *p);

    return 0;
}

5. Subtracting an Integer from Pointer

#include <stdio.h>

int main(){

    int arr[] = {100,200,300,400};
    int *p = &arr[3];

    p = p - 2;

    printf("%d", *p);

    return 0;
}

6. Difference Between Two Pointers

#include <stdio.h>

int main(){

    int arr[] = {1,2,3,4,5};

    int *p = &arr[4];
    int *q = &arr[1];

    printf("Difference = %ld", p - q);

    return 0;
}

7. Pointer Comparison

#include <stdio.h>

int main(){

    int arr[] = {10,20,30};
    int *p = &arr[0];
    int *q = &arr[2];

    if(p < q)
        printf("p is before q");
    else
        printf("p is after q");

    return 0;
}

8. Accessing Array Using Pointer

#include <stdio.h>

int main(){

    int arr[] = {4,8,12};
    int *p = arr;

    for(int i=0; i<3; i++){
        printf("%d ", *(p + i));
    }

    return 0;
}

9. Pointer to Float – Arithmetic

#include <stdio.h>

int main(){

    float arr[] = {1.1, 2.2, 3.3};
    float *p = arr;

    p++;
    printf("%.1f", *p);

    return 0;
}

10. Pointer Arithmetic with Characters

#include <stdio.h>

int main(){

    char str[] = "ABCDEF";
    char *p = str;

    p = p + 4;

    printf("%c", *p);

    return 0;
}

Practice Questions

  1. What happens when you increment an int pointer?
  2. Why can pointer subtraction be done only within arrays?
  3. Explain pointer comparison with an example.
  4. How does pointer arithmetic depend on data type size?
  5. What is the difference between p++ and ++p for pointers?
Practice Task: Create an array of 5 integers and traverse all elements using ONLY pointer arithmetic (no loops with indexes).