Skip to main content

Lesson 2 of 6

Pointer Arithmetic - Step Size & Array Traversal

Pointer arithmetic in C is calculated in units of the **pointed-to type's size**, not in bytes. Adding 1 to an `int*` advances the address by `sizeof(int)` bytes (usually 4). This makes pointer arithmetic the fastest way to traverse an array - the machine instruction is a simple addition of `4` (or whatever element size) to a register.

12 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Pointer Arithmetic - How Step Size Works

Pointer Arithmetic - How Step Size Works
#include <stdio.h>

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

    /* ── Pointer increment - steps by sizeof(*p) bytes ────── */
    printf("*p   = %d  (arr[0])
", *p);     /* 10 */
    p++;
    printf("*p   = %d  (arr[1])
", *p);     /* 20 */
    p += 2;
    printf("*p   = %d  (arr[3])
", *p);     /* 40 */
    p--;
    printf("*p   = %d  (arr[2])
", *p);     /* 30 */

    /* ── Addresses to confirm step size ─────────────────── */
    int *q = arr;
    printf("q+0 = %p  value=%d
", (void *)(q+0), *(q+0));  /* arr[0] */
    printf("q+1 = %p  value=%d
", (void *)(q+1), *(q+1));  /* arr[1], +4 bytes */
    printf("q+2 = %p  value=%d
", (void *)(q+2), *(q+2));  /* arr[2], +8 bytes */

    /* ── Pointer difference - gives number of ELEMENTS ─────── */
    int *start = &arr[0];
    int *end   = &arr[5];   /* one past the last valid element */
    ptrdiff_t count = end - start;   /* ptrdiff_t is the correct type */
    printf("end - start = %td elements
", count);  /* 5 */

    /* ── Traversing array with pointer vs index ─────────── */
    printf("Via index: ");
    for (int i = 0; i < 5; i++) printf("%d ", arr[i]);
    printf("
");

    printf("Via pointer: ");
    for (int *r = arr; r < arr + 5; r++) printf("%d ", *r);
    printf("
");

    /* ── Pointer comparison ──────────────────────────────── */
    int *first = &arr[0], *last = &arr[4];
    if (first < last) printf("first comes before last
");

    /* ── Valid pointer operations ────────────────────────── */
    /* p + n    : advance n elements                         */
    /* p - n    : retreat n elements                          */
    /* p1 - p2  : distance in elements (ptrdiff_t)           */
    /* p < p2   : compare positions (only for same array)    */
    /* *p       : dereference (read/write value)              */

    /* ── INVALID operations ──────────────────────────────── */
    /* p * 2    : multiplication - undefined, compile error   */
    /* p1 + p2  : adding two pointers - undefined             */

    return 0;
}

Common Mistakes

  • Pointing one past the end and dereferencing - `arr + 5` is a valid address for comparison but `*(arr + 5)` is undefined behaviour. You may compare against the one-past-end, but never dereference it.
  • Subtracting pointers from different arrays - `ptr1 - ptr2` is only defined when both pointers point into the same array (or one past its end). Subtracting pointers from different arrays is undefined behaviour.
  • Using `int` to store pointer differences - use `ptrdiff_t` (from `<stddef.h>`) for pointer differences, not `int`. On 64-bit systems, arrays can be larger than `INT_MAX` elements.

Tip

Tip

Practice Pointer Arithmetic Step Size Array Traversal in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Pointer arithmetic automatically scales offsets by sizeof(pointed type) bytes

Practice Task

Note

Practice Task - (1) Write a working example of Pointer Arithmetic Step Size Array Traversal from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Pointer Arithmetic Step Size Array Traversal is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.

Key Takeaways

  • Pointer arithmetic in C is calculated in units of the **pointed-to type's size**, not in bytes.
  • Pointing one past the end and dereferencing - `arr + 5` is a valid address for comparison but `*(arr + 5)` is undefined behaviour. You may compare against the one-past-end, but never dereference it.
  • Subtracting pointers from different arrays - `ptr1 - ptr2` is only defined when both pointers point into the same array (or one past its end). Subtracting pointers from different arrays is undefined behaviour.
  • Using `int` to store pointer differences - use `ptrdiff_t` (from `<stddef.h>`) for pointer differences, not `int`. On 64-bit systems, arrays can be larger than `INT_MAX` elements.

Topics in This Module

0/6 done