Course contentsShow
← C
Lesson 463 of 1,2197. PointersFree lesson

Incrementing pointers with `++`

Incrementing pointers with ++

What you'll learn: How to move a pointer to the next memory address based on its data type.

Stepping through memory

In previous lessons, you learned that a pointer stores a memory address. While a pointer looks like a simple number, incrementing it with the ++ operator behaves differently than incrementing a standard integer.

Think of memory like a long hallway of lockers. If you are standing at Locker 100 and take "one step" forward, you don't necessarily end up at Locker 101. Instead, you land at the start of the very next locker. If the lockers are wide (like a double), your step is large. If the lockers are narrow (like a char), your step is small.

When you use ptr++, C automatically calculates how many bytes to jump based on the type the pointer points to.

Type-aware math

C handles the heavy lifting of memory math for you. If an int takes up 4 bytes of space, incrementing an integer pointer will add 4 to the address, not 1. This ensures you are always pointing at the start of the next valid piece of data rather than landing in the middle of one.

#include <stdio.h>

int main() {
    int numbers[] = {10, 20, 30};
    int *ptr = &numbers[0]; // Pointing to the first element

    printf("Starting address: %p\n", (void*)ptr);
    printf("Value: %d\n", *ptr); // Prints 10

    ptr++; // Moves the pointer to the NEXT integer

    printf("New address:      %p\n", (void*)ptr);
    printf("New value: %d\n", *ptr); // Prints 20

    return 0;
}

Why this matters

This "smart" math is what makes working with arrays possible. Because an array is just a continuous block of memory, ptr++ is the primary way C programmers navigate through lists of data. You don't need to know exactly how many bytes an object occupies; you just tell the pointer to "go to the next one," and C knows exactly how far to leap.

Key takeaway

Incrementing a pointer moves it forward by the size of the data type it points to, ensuring it always lands at the start of the next element.