Pointers in C
What is a pointer? (simple idea)
Think of your computer's memory as a long street of houses, and every house has an address. When you create a variable like int x = 10;, the value 10 is stored inside one of those houses. That house has an address too.
A pointer is simply a variable that stores an address instead of a normal value. So while x holds the number 10, a pointer to x holds "the address where 10 lives." That one idea is the whole foundation of pointers.
A normal variable holds a value. A pointer holds the address of a value.
Declaring and using a pointer
You declare a pointer by putting a * before its name, and you say what type of value it points to.
#include <stdio.h>
int main() {
int x = 10;
int *p = &x; // p stores the address of x
printf("Value of x : %d\n", x);
printf("Address of x : %p\n", &x);
printf("p holds address: %p\n", p);
printf("Value via p : %d\n", *p); // dereference
return 0;
}Value of x : 10
Address of x : 0x7ffee3b2c8
p holds address: 0x7ffee3b2c8
Value via p : 10
Notice that &x and p print the same address — because p is literally holding the address of x. And *p gives back 10, the value at that address. (Your exact address numbers will differ each run — that is normal.)
The two key operators: & and *
Everything with pointers comes down to these two operators, which are opposites:
| Operator | Name | Meaning |
|---|---|---|
& | Address-of | &x = the address where x is stored |
* | Dereference | *p = the value stored at the address in p |
& asks "where is it?" and * asks "what is there?"
Changing a value through a pointer
Because a pointer knows the address of a variable, it can also change that variable's value directly. This is one of the most powerful things pointers do.
#include <stdio.h>
int main() {
int x = 10;
int *p = &x;
*p = 25; // change x through the pointer
printf("x is now %d\n", x);
return 0;
}x is now 25
We never wrote x = 25 directly, yet x became 25 — because *p reaches into x's memory and changes it. This is exactly how functions modify variables passed to them (see call by value vs reference).
NULL pointers and safety
A pointer that does not yet point to anything valid should be set to NULL. This gives you a safe, checkable "empty" state.
#include <stdio.h>
int main() {
int *p = NULL; // points to nothing yet
if (p != NULL)
printf("Value: %d\n", *p);
else
printf("Pointer is empty, not safe to use.\n");
return 0;
}Pointer is empty, not safe to use.
Never use *p on a pointer that is NULL or was never given a valid address — it crashes the program (a "segmentation fault"). Always check first.
Why do we even need pointers?
Pointers feel abstract at first, but C depends on them for real work:
- Changing variables inside functions — a function can update your original variable, not just a copy.
- Arrays and strings — these are handled through addresses under the hood.
- Dynamic memory — asking for memory at runtime with
mallocgives you a pointer. - Efficiency — passing an address is cheaper than copying large data.
Common mistakes
- Using
*pbeforeppoints to a valid address (uninitialised pointer). - Confusing
&and*— remember:&gives an address,*reads a value. - Dereferencing a NULL pointer without checking it first.
- Forgetting that the pointer's type must match the variable's type (an
int *for anint).
Declare two integers a and b, make a pointer point to each, and swap their values using only the pointers (*pa and *pb). Print the results to confirm the swap worked.
Summary
- A pointer stores the address of another variable, not a plain value.
&xgives an address;*preads (or writes) the value at that address.- Through
*pyou can change the original variable directly. - Set unused pointers to
NULLand always check before dereferencing. - Pointers power functions, arrays, strings and dynamic memory in C.
Frequently Asked Questions
What exactly is a pointer in C?
What is the difference between & and * in C?
& operator means "address of" — &x gives you where x is stored. The * operator means "value at" (dereference) — *p gives you the value kept at the address inside pointer p. They are opposites of each other.What is a NULL pointer and why is it useful?
NULL, meaning it points to nothing valid. It is useful as a clear "empty" state you can check before using the pointer, which prevents you from accidentally reading a random memory address.Why do pointers crash programs so often?
* on it.