Skip to main content
Be Bitwise

Pointer Arithmetic

Addresses & Pointers · Memory & Systems track

7 min read

Part ofSystems Programming Companion, week 5

In this module

Moving Through Memory

Pointer arithmetic is how you walk through memory: add or subtract from a pointer and it moves. Low-level code uses this to chew through arrays, iterate over buffers, and reach fields inside structs. It's one of the defining moves in C and C++.

The Key Rule: Stride

Add 1 to a pointer and it does not advance by one byte. It advances by the size of the type the pointer points to. This is the stride.

text
int *p = ...; // suppose p is at address 1000
p + 1; // address 1004 (int is 4 bytes)
p + 2; // address 1008
char *c = ...; // suppose c is at address 2000
c + 1; // address 2001 (char is 1 byte)
c + 2; // address 2002
Pointer TypeSize of Pointed Typep + 1 Advances By
char *1 byte1 byte
int *4 bytes4 bytes
double *8 bytes8 bytes

The compiler picks the right scaling automatically from the pointer's declared type. That's exactly why a pointer's type matters: it controls how arithmetic moves through memory.

Pick a type below and press Next to step from p to p + 1 to p + 2. Every array starts at the same made-up address, 0x1000, so the only thing that changes is how far each step jumps. The bytes are the real little-endian values on x86-64.

p + 1 for different types

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

p = 0x1000

char s[] = "pointer"; char *p = s; p reads one byte.

Step 1 of 3
Key Concept

Pointer arithmetic scales by the size of the pointed-to type. Adding 1 to an int * advances the address by sizeof(int) bytes (typically 4), not by 1. This is called the stride and it makes array traversal natural.

Arrays and Pointers

In C, array indexing is syntactic sugar for pointer arithmetic:

text
int arr[5] = {10, 20, 30, 40, 50};
arr[3] // value: 40
*(arr + 3) // also value: 40 - same operation

arr[3] is just *(arr + 3) in disguise: start at the array's base address, advance by 3 elements (which is 3 * sizeof(int) bytes), dereference. That equivalence is why arrays and pointers feel like the same thing in C, even though they aren't: arr only decays to a pointer to its first element when used in an expression, which is why sizeof arr still gives 20 rather than 8. It's also why the cursed-looking 3[arr] compiles and gives 40 too, since addition doesn't care which side the pointer is on.

Worked Example: Walking an Array of Structs

Stride works for any type, including your own:

c
struct point { int x; int y; }; // 8 bytes
struct point pts[3] = {{1, 2}, {3, 4}, {5, 6}};
struct point *p = pts; // say pts starts at 0x1000
p + 1; // 0x1008, the second point
p + 2; // 0x1010, the third point
(p + 2)->y; // 6

Each step jumps a whole struct point, 8 bytes, landing on the start of the next element. Now cast the same pointer to char * and the stride changes underneath you:

c
char *bytes = (char *)pts;
bytes + 1; // 0x1001, the second byte of pts[0].x

Same address to start with, different type, different arithmetic. This is how low-level code reaches into a structure byte by byte, for parsing a network packet or dumping memory in hex. It's also how bugs creep in when someone casts to the wrong type and every offset after it is quietly scaled by the wrong number.

Under the hood, the compiler turns p + 1 into a plain byte offset. For an int * you'll often see add rax, 4 in the generated assembly, or a scaled address such as [rax+rcx*4] when the index lives in a register. The CPU only ever deals in bytes; the scaling is the compiler's job.

Try it on real memory. This program prints p + 1 for three pointer types, then walks an int array through an unsigned char *, one byte at a time:

Try itChange the values in nums and predict the byte dump before you run it.
C (GCC 9.2.0)

The int * moves 4, the char * moves 1 and the struct point * moves 8. The byte dump holds a surprise, though: 0x11223344 comes out as 44 33 22 11. x86-64 is little-endian, so the least significant byte sits at the lowest address. Read the int whole and you'd never know; walk it with a char * and the real layout shows.

Pointer Subtraction

You can subtract two pointers of the same type to find the number of elements between them:

text
int *start = &arr[0];
int *end = &arr[4];
ptrdiff_t count = end - start; // result: 4 (elements, not bytes)

The result gets scaled by element size, exactly like addition. Its type is ptrdiff_t (from <stddef.h>), a signed integer wide enough for any distance within one array, which an int isn't guaranteed to be on a 64-bit machine. Handy for working out array lengths and the distance between two elements.

Predict: int arr[4] starts at 0x1000. What's the address of &arr[4], and can you use it?

0x1010, which is 4 × 4 bytes past the start. C lets you calculate this "one past the end" pointer and compare against it, which is exactly what a loop like for (p = arr; p != arr + 4; p++) does. What you mustn't do is dereference it: arr[4] isn't part of the array, and reading or writing it touches whatever happens to sit next in memory.

Danger Zone

Pointer arithmetic lets you reach any address your maths can produce. The CPU isn't going to ask if it's valid. Walk one past the end of an array, miscount an offset, fish through the wrong type, and you'll silently corrupt memory or crash.

The most common version is a single character wrong:

c
int arr[4];
for (int i = 0; i <= 4; i++) // should be i < 4
arr[i] = 0;

The last iteration writes four bytes of zeros to arr[4], which is whatever the compiler placed after the array. On the stack, that might be another local variable, a saved register or, a few slots further along, a return address. The program may well keep running as if nothing happened, which is the worst outcome of all. That same raw power is exactly why pointer arithmetic deserves careful eyes, and why Stack-Based Buffer Overflows starts with exactly this mistake.

Here's that loop with a variable parked right after the array, so you can see what the stray write hits:

Try itFix the loop condition and run it again.
C (GCC 9.2.0)

&arr[4] and &balance print the same address, and balance drops from 1000 to 0 without a single line naming it. No crash, no warning. The struct pins the layout so the result repeats every time; with plain locals the compiler picks the neighbour, but the bug's the same.