Pointers
The concept that confuses 90% of beginners — explained so clearly you will wonder why everyone makes it complicated. Pointers are the backbone of linked lists, trees, and every advanced data structure.
Quick answer
A pointer is a variable that holds a memory address. &x gives the address of x, and *p reads or writes the value at the address stored in p. Pointers let functions change the caller's variables and are how linked structures connect.
- int *p declares a pointer to an int.
- * in a declaration means "pointer"; in an expression it means "the value at".
- Passing &x to a function lets it modify x.
#include <stdio.h>
void swap(int *a, int *b) {
int t = *a; *a = *b; *b = t;
}
int main(void) {
int age = 30;
int *p = &age;
*p = 31; /* changes age through the pointer */
printf("age = %d\n", age);
int x = 1, y = 2;
swap(&x, &y);
printf("x = %d, y = %d\n", x, y);
return 0;
}Output
age = 31 x = 2, y = 1
Pointers are the single most important concept in C — and the single most misunderstood. Most tutorials throw symbols at you and hope something sticks. We are going to do the opposite: build the idea from the ground up, one layer at a time, until it feels completely obvious.
After this lesson, linked lists, trees, graphs, and dynamic memory will all make sense. Every advanced data structure in DSA uses pointers internally. This lesson is the key that unlocks the rest.
Every Variable Has a Home Address
Go back to what we learned in Lesson 1. RAM is like a long street of tiny houses. Each house has a unique address. When you create a variable, the computer picks an empty house, stores your value there, and gives that house number a name — the variable name.
🏠 The house and address analogy
The address tells you where to find them.
The contents are who lives there.
The address tells you where to find it.
The value is what is stored there.
The variable name age is just a convenient label for humans. Internally, the computer only works with the address 0x1004. The name exists only in your source code — it disappears when the program compiles.
Here is how you see a variable's address in C using the & operator:
scanf("%d", &age) means "read a number and store it at the address of age." Now you know exactly why.What is a Pointer?
A pointer is a variable that stores a memory address instead of a regular value.
A normal variable stores something like 25, 3.14, or 'A'. A pointer variable stores something like 0x1004 — the address of where some other variable lives. Think of a pointer as a piece of paper with someone's home address written on it. The paper is not the house. It just tells you where the house is.
p stores the address of age (0x1004). p itself also lives somewhere in memory (0x2008). Following p leads you to age.
The Two Operators — & and *
Pointers use two operators. These two symbols are what confuse most people — because they look the same as other uses of & and * in C but mean completely different things depending on context. Let us nail this once and for all.
int age = 25int *pp = &age*p*p = 30In a declaration like
int *p — the * means "p is a pointer to int".In an expression like
*p = 30 — the * means "go to the address p holds".Same symbol. Different context. Different meaning. This is the #1 source of pointer confusion.
Pointer Types — Why They Matter
Every pointer has a type — int*, float*, char*. The type tells the compiler how many bytes to read when you dereference. An int* reads 4 bytes. A char* reads 1 byte. If you use the wrong pointer type, you read the wrong amount of memory and get garbage data.
Pointer Arithmetic — Moving Through Memory
You can add and subtract integers from pointers — but the result is not what you might expect. When you do p + 1, the pointer does not move by 1 byte. It moves by 1 × sizeof(type) bytes. An int pointer moves 4 bytes forward. A char pointer moves 1 byte forward. This is deliberate — it lets you step through arrays cleanly.
arr[2], C internally translates it to *(arr + 2). The array name is a pointer to the first element. Square bracket notation is just cleaner syntax for pointer arithmetic. This is why arrays and pointers are so deeply connected in C.Pointers and Functions — Pass by Reference
This is where pointers become genuinely powerful and practically useful. In C, when you pass a variable to a function normally, the function gets a copy — it cannot change the original. This is called pass by value. When you pass the address of a variable, the function can reach back and change the original directly. This is called pass by reference.
Classic example — swap two numbers
The swap function is the most famous pass-by-reference example. It cannot work without pointers in C. This exact function is used inside sorting algorithms.
Pointers and Arrays — The Deep Connection
When you pass an array to a function in C, you are actually passing a pointer to its first element. The entire array is not copied — only the address is passed. This means functions can modify arrays directly, and it is also why arrays are fast to pass around even when they are huge.
The NULL Pointer — A Pointer That Points to Nothing
NULL is a special value you can assign to a pointer to say "this pointer currently points to nothing." It is the number 0. In linked lists and trees, NULL is how you mark the end of a chain — "there is no next node."
*p when p is NULL, your program crashes immediately with a Segmentation Fault. Always check if (p != NULL) before reading from a pointer if there is any chance it could be NULL. This rule saves you hours of debugging.Common Pointer Mistakes — And How to Avoid Them
Why Pointers Unlock All of DSA
Now that you understand pointers, let us connect this to what is coming. Every data structure from Lesson 6 onwards uses pointers to link pieces of data together. Here is a quick preview:
You Are Ready for Lesson 6
You now understand the most important and most feared concept in C programming. Pointers are not magic — they are just variables that hold addresses, and two operators: & to get an address, * to follow one.
In Lesson 6 we build our first truly dynamic data structure — Linked Lists. Everything you just learned about pointers will be used immediately. A linked list is nothing more than nodes connected by pointers.
🎯 Key Takeaways
- ✓Every variable lives at a specific address in RAM. The & operator gives you that address
- ✓A pointer is a variable that stores a memory address — nothing more, nothing less
- ✓int *p declares p as a pointer to int. p = &age stores the address of age in p
- ✓The * symbol has two meanings: in declarations it means "pointer type", in expressions it means "go to that address"
- ✓*p reads the value at the address p holds. *p = 10 writes 10 to that address
- ✓Pointer arithmetic: p+1 moves by sizeof(type) bytes — not by 1 byte
- ✓Pass by reference: passing &variable lets a function modify the original. Pass by value only passes a copy
- ✓NULL means the pointer points to nothing. Always check for NULL before dereferencing
- ✓Array name is a pointer to the first element. arr[i] and *(arr+i) are identical
- ✓Every data structure from here — linked lists, trees, graphs — is built by connecting nodes with pointers
Quick check
p holds the address of age. What does *p = 5 do?
HELP US IMPROVE
How was this lesson?
Your note is shared with the Chaduvuko team for review. Please don’t include personal or sensitive information.
Spotted a mistake or something unclear? Suggest a fix on GitHub or email us. Select the text first and it is quoted for you.
Discussion
0Have a better approach? Found something outdated? Share it — your knowledge helps everyone learning here.
