Pointers
A pointer is a variable whose value is the address of another object. Its type records what kind of object lives at that address, which tells the compiler how many bytes to read and how to interpret them.
Detailed Explanation
Every object in a running C program lives somewhere in memory, and every location has a number called its address. A pointer stores one of those numbers. That is the whole idea. The complexity comes from the type system built on top of it, not from the concept.
Two operators connect a pointer to the object it points at. &x produces the address of x. *p goes to the address stored in p and gives you the object there. *p is an lvalue: you can read it or assign to it, and assigning through it changes the original object.
int score = 42;
int *p = &score; /* p holds the address of score */
*p = 43; /* score is now 43 */
Declaration syntax follows a rule called declaration-mirrors-use: int *p; means "the expression *p has type int", so p is a pointer to int. This is also why int *a, b; declares one pointer and one plain int. The * binds to the declarator, not to the type. Most C style guides write int *p rather than int* p for exactly this reason.
The pointer's type is not decoration. It determines how many bytes a dereference reads and how those bytes are interpreted, and it determines how far p + 1 moves. Reading an object through a pointer of an incompatible type violates the strict aliasing rule and is undefined behavior; memcpy or a union is the supported way to reinterpret bytes.
Why do pointers exist at all? Two reasons, both practical.
First, out-parameters. C passes every argument by value, always. A function that receives an int receives a copy and cannot change the caller's variable. Passing &x gives the function the address, so it can write through it. This is how scanf fills your variables and how a function returns both a result and an error code.
Second, referring to something large without copying it. Passing a 4 KB struct by value copies 4 KB on every call. Passing a pointer copies eight bytes. Arrays lean on this too: an array name used in an expression decays to a pointer to its first element, so array arguments are never copied.
NULL is the null pointer constant, a value guaranteed to compare unequal to the address of any object. It is the conventional "points at nothing" marker — a malloc that failed, the end of a linked list, an optional argument the caller skipped. Dereferencing it is undefined behavior. On a typical hosted system with memory protection you get a segmentation fault, which is the pleasant outcome because it is loud. On a small embedded target address zero may be readable memory, and the program quietly produces nonsense. The standard promises nothing either way, which is why you check for NULL rather than relying on a crash.
A pointer that has never been assigned is a different hazard. It is not NULL; it holds whatever bits were on the stack, and even reading its value is undefined behavior. Initialize pointers at the point of declaration.
The cost of pointers is not speed — an indirection is cheap. The cost is that the compiler can no longer prove much about your program: lifetimes, ownership, and aliasing all become your responsibility, and every one of those has a matching class of bug.
Code Examples
#include <stdio.h>
/* An out-parameter. The function returns success/failure and writes the
result through a pointer, because C cannot return two things at once. */
static int safe_divide(int numerator, int denominator, int *out_result)
{
if (denominator == 0 || out_result == NULL) {
return 0; /* caller passed something unusable */
}
*out_result = numerator / denominator; /* writes the CALLER's variable */
return 1;
}
int main(void)
{
int answer = 0;
if (safe_divide(84, 2, &answer)) { /* &answer: the address of answer */
printf("%d\n", answer); /* prints: 42 */
} else {
printf("division failed\n");
}
return 0;
}#include <stdio.h>
int main(void)
{
int value = 7;
int *p = &value; /* initialize at declaration; never leave it wild */
int **pp = &p; /* a pointer to a pointer is not a special case */
printf("%d %d %d\n", value, *p, **pp); /* prints: 7 7 7 */
**pp = 9; /* two hops, then write */
printf("%d\n", value); /* prints: 9 */
/* The type decides what a dereference means. Same address, different
interpretation, which is why casting between pointer types is not free. */
printf("%zu %zu\n", sizeof *p, sizeof p);
/* on a typical 64-bit machine prints: 4 8
sizeof *p is the size of an int; sizeof p is the size of a pointer. */
p = NULL; /* an explicit "points at nothing" marker */
if (p != NULL) { /* guard before every dereference of a maybe-null pointer */
printf("%d\n", *p);
}
return 0;
}Common Gotchas
Dereferencing `NULL` is undefined behavior, not a guaranteed crash
on a desktop OS it usually faults, but the standard does not promise that, and on freestanding targets address zero may be ordinary memory. Check the pointer; do not rely on the hardware to catch you.
An uninitialized pointer is worse than a null one
its value is indeterminate, so even reading it is undefined behavior and comparing it to `NULL` proves nothing. Write `int *p = NULL;` if you have nothing better to give it.
A pointer does not keep its target alive
returning the address of a local variable gives the caller a dangling pointer the moment the function returns. C has no ownership tracking; you do that in your head or in your naming conventions.