Back to Blog

C in 100 Seconds: Walk an Array With a Pointer — Pointer Arithmetic | Episode 18

Daryl WongDaryl Wong

Video: C in 100 Seconds: Walk an Array With a Pointer — Pointer Arithmetic | Episode 18 by Taught by Celeste AI - AI Coding Coach

Watch full page →

Pointer Arithmetic in C — Walk Memory Like a Pro

C in 100 Seconds, Episode 18


In C, arrays and pointers are deeply connected. An array name is just a pointer to its first element. Once you understand that, pointer arithmetic becomes second nature.

The Setup

int nums[] = {10, 20, 30, 40, 50};
int *ptr = nums;

No ampersand needed. nums already points to the first element. ptr and nums now refer to the same address in memory.

Adding to a Pointer

Here's the key insight: ptr + 1 doesn't move one byte — it moves one element.

An int is 4 bytes. So ptr + 1 jumps 4 bytes forward to land on the next integer.

*ptr        // 10  (first element)
*(ptr + 1)  // 20  (second element)
*(ptr + 3)  // 40  (fourth element)

The compiler knows the type of the pointer and scales the offset automatically. You think in elements, not bytes.

Walking an Array

You can iterate through an entire array using pointer arithmetic:

for (int i = 0; i < 5; i++) {
  printf("*(ptr + %d) = %d\n", i, *(ptr + i));
}

This produces:

*(ptr + 0) = 10
*(ptr + 1) = 20
*(ptr + 2) = 30
*(ptr + 3) = 40
*(ptr + 4) = 50

*(ptr + i) is equivalent to nums[i]. In fact, the bracket notation is just syntactic sugar — the compiler converts nums[i] to *(nums + i) behind the scenes.

Why Does This Matter?

Pointer arithmetic is how C navigates memory at a low level. It's used everywhere:

  • String processing — walking through characters one by one
  • Dynamic memory — managing blocks allocated with malloc
  • Data structures — linked lists, trees, and buffers all rely on pointer manipulation
  • Performance — pointer iteration can be faster than index-based access in tight loops

The Rule

Adding n to a pointer moves it forward by n * sizeof(type) bytes.

An int* moves 4 bytes per step. A char* moves 1 byte. A double* moves 8. The type determines the stride.

Full Code

#include <stdio.h>

int main() {
  int nums[] = {10, 20, 30, 40, 50};
  int *ptr = nums;

  printf("ptr points to: %d\n", *ptr);
  printf("ptr + 1: %d\n", *(ptr + 1));
  printf("ptr + 3: %d\n", *(ptr + 3));

  printf("\nWalking the array:\n");
  for (int i = 0; i < 5; i++) {
    printf("  *(ptr + %d) = %d\n", i, *(ptr + i));
  }

  return 0;
}

Compile and run:

gcc ptrarith.c -o ptrarith
./ptrarith

Next episode: Pass by Reference — using pointers to modify variables inside functions.

Student code: github.com/GoCelesteAI/c-in-100-seconds/episode18