Learn C
#include "pointersmemory/pointer_arithmetic.h"

Pointer Arithmetic in C

#concept

Pointer arithmetic moves in units of the pointed-to type: p + 1 advances by sizeof(*p) bytes, not one byte — so int* steps by 4, char* by 1. This makes pointers and arrays two spellings of one idea (a[i] is exactly *(a + i)), and loops over buffers read naturally as pointer walks.

The rules are strict: arithmetic is defined only within (or one past) the same array object, and differencing two pointers (q - p) yields the element count between them as ptrdiff_t. Stray outside one object and the behavior is undefined, however sensible the addresses look.

#how it works
1

Scaled by element size

p + 1 means next element, not next byte — the compiler multiplies by sizeof(*p) automatically.

2

Arrays are pointer math

a[i] is *(a + i) by definition — indexing and pointer arithmetic are literally the same operation.

3

Differencing counts elements

q - p (same array) gives the element distance as ptrdiff_t — the foundation of length computations.

4

Stay in-bounds

Arithmetic within an array (plus one-past-the-end for loop sentinels) is defined; wandering elsewhere is undefined behavior.

#example
#include <stdio.h>
 
int main(void) {
int a[] = {10, 20, 30};
int *p = a;
printf("%d\n", *(p + 2));
printf("%td\n", (p + 2) - p);
return 0;
}
Output
30
2
1p points at a[0]; p + 2 advances two ints (8 bytes on typical platforms) to a[2].
2Dereferencing yields 30 — identical to writing a[2].
3Differencing the pointers counts 2 elements between them, printed portably with %td.
#use cases
Walking buffers with pointer loops instead of indexes
Parsing binary data by advancing typed pointers through records
Computing lengths and offsets via pointer differences
Understanding that a[i] and *(a+i) are interchangeable everywhere

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C yourself — building beats watching.