Lesson 23 / 36

Pointer to Pointer & NULL

Indirection, one more level, and how pointers fail safely.

A pointer to a pointer

int **pp holds the address of an int *. Dereferencing once (*pp) gives the inner pointer; twice (**pp) gives the actual int. Common in dynamic 2D arrays and functions that need to reassign a caller's pointer.

NULL means "points nowhere"

An uninitialised pointer holds garbage. Assigning NULL explicitly marks it as pointing nowhere, and it's good practice to check before dereferencing.

int *p = NULL;

if (p != NULL) {
    printf("%d\n", *p);
} else {
    printf("p points nowhere\n");
}

Output:

p points nowhere

Two classic pointer bugs

Wild pointer: used before being assigned a valid address. Dangling pointer: still points to memory that has already been freed. Both cause undefined behavior — always initialise, and set to NULL after freeing.