C

Pointer Arithmetic in C Programming

Pointer Arithmetic is the set of valid mathematical operations that can be performed on a pointer to navigate through memory. Unlike standard arithmetic, where $1 + 1 = 2$, pointer arithmetic is scaled by the size of the data type the pointer is pointing to.

This concept is the backbone of how C handles arrays, strings, and dynamic memory buffers internally.

1. The Theory: Scaled Arithmetic

When you perform arithmetic on a pointer, you are not just adding numbers to a memory address; you are moving the pointer by a specific number of data units.

The fundamental rule of pointer arithmetic is:

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

Why is it scaled?

If you have an int pointer (ptr) at address 1000, and an int takes 4 bytes in memory, then ptr + 1 must point to 1004 (the start of the next integer). If it simply went to 1001, it would be pointing to the middle of the first integer, which would result in corrupted data.

2. Valid Pointer Operations

C allows four main types of arithmetic operations on pointers:

A. Increment (++)

Incrementing a pointer moves it to point to the next memory location of its type.

  • Example: ptr++ on a double pointer (8 bytes) increases the address by 8.

B. Decrement (--)

Decrementing a pointer moves it to point to the previous memory location of its type.

  • Example: ptr-- on a char pointer (1 byte) decreases the address by 1.

C. Addition and Subtraction of Integers (+, -)

You can add or subtract an integer value to a pointer to "jump" multiple elements forward or backward.

  • Syntax: ptr = ptr + 3; (Moves the pointer 3 elements ahead).

D. Pointer Subtraction (Distance)

You can subtract one pointer from another if they point to the same array.

  • Result: The result is not an address, but the number of elements between the two pointers.

  • Example: int distance = ptr2 - ptr1;

3. Pointer Arithmetic and Arrays

In C, the name of an array acts as a pointer to its first element. Therefore, array indexing is actually a "shorthand" for pointer arithmetic.

  • arr[i] is internally processed by the compiler as *(arr + i).

  • arr[0] is the same as *arr.

  • arr[1] is the same as *(arr + 1).

4. Practical Code Example

This program demonstrates how pointer arithmetic can be used to traverse an array without using traditional indexing.


#include <stdio.h>

int main() {
    int arr[] = {10, 20, 30, 40, 50};
    int *ptr = arr; // Points to arr[0]

    printf("Initial address: %p, Value: %d\n", (void*)ptr, *ptr);

    // 1. Incrementing the pointer
    ptr++; 
    printf("After ptr++: Address: %p, Value: %d\n", (void*)ptr, *ptr);

    // 2. Adding an integer
    ptr = ptr + 2; 
    printf("After ptr + 2: Address: %p, Value: %d\n", (void*)ptr, *ptr);

    // 3. Pointer Subtraction (Distance)
    int *startPtr = &arr[0];
    int *endPtr = &arr[4];
    printf("\nDistance between end and start: %ld elements\n", endPtr - startPtr);

    // 4. Traversing using arithmetic
    printf("\nTraversing array using pointers:\n");
    int *p = arr;
    for(int i = 0; i < 5; i++) {
        printf("Element %d: %d\n", i, *(p + i));
    }

    return 0;
}

5. Invalid Operations (What NOT to do)

Not all math is allowed on pointers. The following will cause compilation errors:

  • Addition of two pointers: ptr1 + ptr2 makes no sense (like adding two house numbers).

  • Multiplication/Division of pointers: ptr1 * 5 or ptr1 / ptr2 is illegal.

  • Pointer math on void *: Since void has no size, the compiler doesn't know how many bytes to skip. You must type-cast a void pointer before performing arithmetic.

6. Key Takeaways

  • Address vs. Value: ptr + 1 changes the address the pointer holds. *ptr + 1 changes the actual data stored at that address.

  • Portability: Pointer arithmetic makes code portable. You don't need to know if an int is 2, 4, or 8 bytes on a specific machine; the compiler handles the scaling automatically.

  • Precedence: Be careful with *ptr++. Due to operator precedence, this increments the pointer after fetching the value, not the value itself.

Upcoming Course
Upcoming Course
Learn More
Instructor Tips
Instructor Tips
View Tips
Join Community
Join Community
Join Now