Pointer Arithmetic in C
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.
Scaled by element size
p + 1 means next element, not next byte — the compiler multiplies by sizeof(*p) automatically.
Arrays are pointer math
a[i] is *(a + i) by definition — indexing and pointer arithmetic are literally the same operation.
Differencing counts elements
q - p (same array) gives the element distance as ptrdiff_t — the foundation of length computations.
Stay in-bounds
Arithmetic within an array (plus one-past-the-end for loop sentinels) is defined; wandering elsewhere is undefined behavior.
#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;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C yourself — building beats watching.