← Pointers and MemoryC PROGRAMMING 1.2.B

Pointers in Action

Reading/writing via pointer, reassignment, live-value reflection, a pointer’s own size, NULL.

StatusCollected
NotesChapter test: 5/5

Eight pieces building directly on Chapter A — actually using a pointer to read and write, watching a value change through two different names at once, reassigning where a pointer points, and NULL as the deliberate "points at nothing" state.

Writing through a pointer

int x = 10;
int *p = &x;

*p = 20;              /* writes through the pointer */
printf("%d\n", x);   /* 20 — x itself changed, because p points at x's address */

This is the core trick — x and *p are two different names for the exact same memory. Change one and the other reflects it immediately, because there's only one location involved.

Reassignment

int a = 1, b = 2;
int *p = &a;
p = &b;                /* p now points at b instead — a is untouched */

A pointer's own size

sizeof(p) doesn't depend on what p points to — on a 64-bit system every pointer is 8 bytes, whether it's int *, char *, or a pointer to some huge struct. What varies by type is how many bytes get read or written on a dereference, not the size of the address itself.

NULL

int *p = NULL; means "points at nothing, deliberately." Address 0 is reserved by the OS specifically so dereferencing a NULL pointer reliably crashes instead of silently corrupting something — a NULL check before dereferencing is one of the cheapest habits to build early and one of the easiest to skip under pressure.

Notes from readers

Comments — via GitHub