Loading...

C essentialsPointers & Memory

Pointer arithmetic & arrays

12 min lesson

Next
  • Pointer arithmetic lets you move a pointer forward or backward in memory by whole elements, not by bytes.
  • It matters because arrays and pointers are closely related in C, and pointer arithmetic is how you walk through an array or a buffer.
  • Think of pointer arithmetic as stepping stones in a row: p + 1 moves to the next stone, not one centimeter forward.
  • When you add 1 to an int pointer, the address increases by sizeof(int) bytes, usually 4.
  • An array name used in most expressions becomes a pointer to its first element, so arr is like &arr[0].
  • You can index a pointer like an array: p[i] means the same as *(p + i).
  • The computer multiplies the offset by the size of the pointed-to type to find the new address.
  • The syntax is: p + n, p - n, p++, p--, and p1 - p2 (distance in elements between two pointers in the same array).
  • A realistic use: scanning a buffer of bytes, walking a string, or iterating over an array with a pointer instead of an index.
  • A surprising detail: subtracting two pointers is only valid when they point into the same array; otherwise the result is undefined.
  • The example program walks an int array with a pointer, prints each element, and shows how pointer subtraction gives the element count.
Keep in mind
  • Pointer arithmetic moves in units of the pointed-to type, not bytes.
  • An array name behaves like a pointer to its first element in most expressions.
  • p[i] and *(p + i) mean the same thing.
  • Only subtract pointers that point into the same array.
  • You are unlocking the real power of C. Keep it up!
main.c
#include <stdio.h>

int main(void) {
    // An array of five ints
    int values[5] = {10, 20, 30, 40, 50};
    // A pointer to the first element
    int *p = values;

    // Walk the array using pointer arithmetic
    for (int i = 0; i < 5; i++) {
        // p + i points to the i-th element; * reads it
        printf("values[%d] = %d\n", i, *(p + i));
    }

    // Move the pointer to the last element
    int *last = values + 4;
    // Print the value there
    printf("Last: %d\n", *last);

    // Subtract two pointers to get the distance in elements
    printf("Distance: %ld\n", (long)(last - p));

    return 0;
}

What happens: values[0] = 10 values[1] = 20 values[2] = 30 values[3] = 40 values[4] = 50 Last: 50 Distance: 4

Breaking it down

Array and pointer

int *p = values;

The array name values becomes a pointer to its first element. p and values now refer to the same place.

Pointer offset

*(p + i)

Moves i elements forward from p and dereferences the result. The compiler scales i by sizeof(int).

Indexing a pointer

p[i]

Exactly the same as *(p + i). Both forms are idiomatic and produce the same address.

Last element

values + 4

Points to the element at index 4, the last one. Valid indices for a 5-element array are 0 to 4.

Pointer subtraction

last - p

Gives the number of elements between the two pointers, here 4. Both must point into the same array.

Pointer arithmetic examples

ExpressionMeaningAddress change
p + 1Next element+ sizeof(*p) bytes
p - 1Previous element- sizeof(*p) bytes
p++Advance then useMoves forward one element
p[i]Element at index iSame as *(p + i)
q - pDistance in elementsValid in same array

A helpful analogy

Pointer arithmetic is like stepping stones across a stream. Each stone is one element wide. Moving to the next stone is +1, not moving one centimeter. The size of the stone is handled for you, just like the compiler multiplies by sizeof for you.

Nesting rules

  • 1.Pointer arithmetic is scaled by the size of the pointed-to type, not by bytes.
  • 2.Only add or subtract within the bounds of the same array (one past the end is allowed for comparisons).
  • 3.Subtracting two pointers is only meaningful when they point into the same array.
  • 4.Do not dereference one-past-the-end pointers; they are useful only for comparison.

Common mistakes

Assuming p + 1 moves one byte

Avoid

char *c = (char *)p; c + 1 // one byte

Use instead

p + 1 // one int forward

Pointer arithmetic is scaled by the type size. An int pointer moves sizeof(int) bytes, usually 4, not 1.

Going past the end of the array

Avoid

int *end = values + 5; *end = 99; // out of bounds

Use instead

int *end = values + 4; *end = 99;

Indices 0 to 4 are valid for a 5-element array. Writing index 5 is undefined behavior.

Subtracting pointers from different arrays

Avoid

int a[3], b[3]; b - a // undefined

Use instead

int a[3]; &a[2] - &a[0] // 2

Pointer subtraction is only defined within the same array. Different arrays give meaningless results.

Quick tips

  • •Use p[i] when it reads more clearly than *(p + i).
  • •Print the pointer and the value while debugging to see how it moves.
  • •Remember that one-past-the-end is allowed for comparison but not for dereferencing.
  • •Try replacing the loop index with p++ to walk the array directly.
  • •Use sizeof to confirm the step size: sizeof(*p).
Previous
Next