Skip to main content
Be Bitwise
CPointers Deep Dive

Pointer Arithmetic

C lets you do arithmetic directly on pointers, which is wonderful when you're walking an array and dreadful when you're off by one. The catch: adding 1 doesn't add a byte. It advances the pointer by however many bytes the pointed-to type takes up.

How It Works

c
int arr[] = {10, 20, 30, 40, 50};
int *p = arr; // p points to arr[0]
printf("%d\n", *p); // 10
printf("%d\n", *(p + 1)); // 20
printf("%d\n", *(p + 2)); // 30

An int is typically 4 bytes, so p + 1 jumps four bytes ahead and lands on the next integer.

Worked Example: Following the Addresses

Say arr starts at address 0x1000. That's illustrative (real stack addresses are far bigger), but the steps are the same. Each int takes 4 bytes, so the five elements sit back to back:

ExpressionAddressWhat's there
p, same as arr + 00x100010
p + 10x100420
p + 20x100830
p + 40x101050
p + 50x1014one past the end: valid to form, never to dereference

The compiler turns p + i into "the address in p, plus i times the size of the pointed-to type". For p + 4 that's 4 × 4 = 16 bytes, and 16 is 0x10, so 0x1000 + 0x10 = 0x1010. If arr held doubles instead, the same expression would move 4 × 8 = 32 bytes and land on 0x1020.

Step through both versions below. The addresses match the table, and the bytes are what x86-64 really stores, least significant byte first. Watch where p + 5 ends up: one past the last element, a legal address to hold and compare against, with nothing of yours behind it.

Walking p through an array

Starts at 0x1000; small numbers under each byte are the last two hex digits of its address.

p = 0x1000

p = arr. *p reads the four bytes 0a 00 00 00, which is 10.

Step 1 of 5

You can watch the step size change with the type. Casting both pointers to char * before subtracting gives the distance in bytes, since a char is one byte by definition.

Try itPredict the three gaps before you run it, then add a long long array and guess its gap too.
C (GCC 9.2.0)

The addresses change from run to run, but the gaps don't: 4, 8 and 1 on the 64-bit Linux machine behind the runner.

Array Indexing Is Pointer Arithmetic

In C, arr[i] and *(arr + i) are exactly the same thing. Array indexing is just syntactic sugar wrapped around pointer arithmetic:

c
arr[3] // same as *(arr + 3)
*(arr + 3) // same as arr[3]
3[arr] // also valid! (but please never write this)
Key Concept

Arrays in C aren't objects in the OOP sense. In most contexts they "decay" into a pointer to their first element, which is exactly what gets passed when you hand an array to a function. The receiver has no idea how long it is. That's why every C function taking an array also takes a length, by hand.

Iterating with Pointers

A common idiom: walk an array with a pointer rather than an index counter.

c
int arr[] = {1, 2, 3, 4, 5};
int *end = arr + 5;
for (int *p = arr; p < end; p++) {
printf("%d ", *p);
}

You'll see this in hot inner loops because compilers can sometimes squeeze pointer-bumping into tighter machine code than index arithmetic — though the gap has narrowed considerably with modern optimisers.

Pointer Subtraction

Subtracting one pointer from another tells you how many elements apart they are. Note: elements, not bytes.

c
#include <stddef.h> // for ptrdiff_t
int arr[] = {10, 20, 30, 40, 50};
int *start = &arr[0];
int *middle = &arr[2];
ptrdiff_t diff = middle - start; // 2 (not 8)

That gives you a neat way to measure a string: walk a pointer to the terminating '\0' and subtract where you started.

Try itRun it, then make the loop stop at a 'z' instead of the terminator and watch it wander off the end.
C (GCC 9.2.0)

None of those strings contains a z, so the broken loop reads whatever bytes follow in memory. It might print a bigger number, or it might crash. That's undefined behaviour doing what undefined behaviour does.

What Arithmetic Is Allowed

Only a handful of operations make sense on pointers, and the standard spells them out:

  • Pointer plus or minus an integer. The result has to stay inside the same array, or point exactly one past its end.
  • Pointer minus pointer. Both must point into the same array (or one past it). You get a ptrdiff_t counting elements.
  • Comparing with <, >, <=, >=. Again only meaningful within one array, which is why the loop above can test p < end. Equality (==, !=) works between any two pointers of compatible type, and against NULL.
  • Pointer plus pointer won't compile. Adding two addresses doesn't give you an address of anything.

Arithmetic on a void * is also off-limits in standard C, because void has no size to step by. GCC accepts it as an extension and steps one byte at a time, so code that relies on it won't build cleanly elsewhere; cast to char * when you mean bytes.

Danger: Out-of-Bounds Access

C does zero bounds checking. Walk a pointer past the end of an array, or step backwards before its start, and you've wandered into undefined behaviour:

c
int arr[5] = {1, 2, 3, 4, 5};
int *p = arr + 10; // undefined behaviour already: only arr + 5 (one past the end) may be formed
printf("%d\n", *p); // could print anything, or crash

Decades of buffer overflow CVEs all start exactly here.