Pointers basics
15 min lesson
- A pointer is a variable that stores the memory address of another value, not the value itself.
- Pointers matter because they let you share data between functions, build data structures like lists and trees, and manage memory directly.
- Think of a pointer as a sticky note with a house address written on it. The note is not the house, but it tells you exactly where to find it.
- An address is the number that identifies a byte in memory; every variable has one.
- The & operator gives you the address of a variable: &age.
- The * operator, called dereference, reads or writes the value at an address: *p.
- The type of a pointer says what kind of value lives at that address: int *p means p points to an int.
- A pointer declaration looks like int *p; and should be initialized, for example int *p = &age;.
- The computer stores the address as a number (often 8 bytes on modern systems) and uses the type to know how many bytes to read or write when you dereference.
- A realistic use: a function that needs to modify the caller's variable, or a linked list node that points to the next node.
- A surprising detail: a pointer that has not been initialized points to a random place; dereferencing it is undefined behavior and often crashes.
- The example program stores a number, takes its address, prints the address, reads the value through the pointer, and changes the value through the pointer.
- A pointer holds an address; the type tells it what lives there.
- Use & to get an address and * to read or write the value at that address.
- Always initialize pointers, or set them to NULL when they point to nothing.
- Never dereference a NULL or uninitialized pointer.
- Pointers are the heart of C. Take your time; you are doing great!
#include <stdio.h>
int main(void) {
// A normal int variable with a value
int age = 30;
// A pointer that stores the address of age
int *p = &age;
// Print the value of age directly
printf("age: %d\n", age);
// Print the address stored in p using %p
printf("address in p: %p\n", (void *)p);
// Read the value at that address with *
printf("value via *p: %d\n", *p);
// Change age through the pointer
*p = 31;
// Print the new value of age to prove it changed
printf("age after change: %d\n", age);
return 0;
}What happens: age: 30 address in p: 0x7ffd5e8a9b4c value via *p: 30 age after change: 31
Breaking it down
Normal variable
int age = 30;Creates an int in memory and stores 30. The computer knows where it lives, but you usually do not need to.
Pointer declaration
int *p = &age;Declares a pointer to int and stores the address of age in it. The & gives the address, and * in the type means 'pointer to'.
Printing an address
printf("%p", (void *)p);%p prints a pointer value. The cast to void * is the correct way to pass any pointer to %p.
Dereferencing
*pReads the value at the address in p. Since p points to age, *p is 30.
Writing through a pointer
*p = 31;Stores 31 at the address in p, which is age. Afterward, age becomes 31.
Pointer operators
| Operator | Name | Purpose | Example |
|---|---|---|---|
| & | Address-of | Get a variable's address | int *p = &age; |
| * | Dereference | Read/write value at address | *p = 31; |
| %p | Format specifier | Print a pointer | printf("%p", (void *)p) |
| NULL | Null pointer | Point to nothing | int *p = NULL; |
A helpful analogy
A pointer is like a sticky note with a house address. The note is small and easy to pass around, but it is not the house. When you want to visit, you read the address and go there. If the address is wrong or blank, you end up nowhere useful, which is what happens with an uninitialized or NULL pointer.
Nesting rules
- 1.Initialize every pointer before use, either to a valid address or to NULL.
- 2.The pointer type must match the type it points to; int * should point to an int.
- 3.Use %p with a (void *) cast to print a pointer portably.
- 4.Never dereference a NULL or uninitialized pointer; check for NULL before *.
Common mistakes
Using an uninitialized pointer
Avoid
int *p;
*p = 5; // undefinedUse instead
int x;
int *p = &x;
*p = 5;p holds a random address. Writing through it corrupts unknown memory or crashes. Always initialize pointers.
Confusing * in a declaration with dereference
Avoid
int *p = 5; // sets p to address 5Use instead
int x = 5;
int *p = &x;In a declaration, * means 'pointer to'. You must store an address, not a plain value.
Dereferencing NULL
Avoid
int *p = NULL;
printf("%d", *p); // crashUse instead
if (p != NULL) printf("%d", *p);NULL points to nothing. Dereferencing it is undefined behavior and usually crashes the program.
Quick tips
- •Read int *p as 'p is a pointer to int', not 'p is an int'.
- •Set pointers to NULL when they do not point anywhere yet.
- •Print addresses with %p and a (void *) cast to see them safely.
- •Try changing age through *p in the example and watch the printed age change.
- •Use -Wall -Wextra; the compiler warns about many pointer mistakes.