What is a pointer?
In C, a pointer is a variable that stores the memory address of another variable.
Instead of storing a value directly, it stores where the value is located in memory.
A pointer is like a house address instead of the house itself.
Why do we use pointers?
- ⚡ Efficient memory management
- 📦 Working with arrays and strings
- 🔁 Passing large data to functions efficiently
- 🧠 Dynamic memory allocation
- 🔗 Building complex data structures (linked lists, trees)
Pointer syntax
// normal variable int x = 10; // pointer variable int *ptr = &x;
Here:
&xgives the address of x*ptrstores that address
Dereferencing a pointer
You can access the value stored at an address using the * operator.
int x = 10;
int *ptr = &x;
printf("%d", *ptr); // prints 10
How pointers work in memory
- Variable is stored in memory with an address
- Pointer stores that address
- Dereferencing accesses the value at that address
Pointer example
int a = 5;
int *p = &a;
*p = 10;
printf("%d", a); // prints 10
Changing *p changes the original variable.
Types of pointers
- Null pointers
- Void pointers
- Wild pointers
- Dangling pointers
Common mistakes
- Using uninitialized pointers
- Dereferencing null pointers
- Memory leaks
Pointers give powerful control over memory, but incorrect usage can cause crashes or undefined behavior.
Summary
Pointers in C are variables that store memory addresses instead of values. They allow direct memory access and are essential for advanced programming concepts.
Once understood, pointers unlock the full power of C programming.