Pointers and Memory
The most feared, most powerful concept in C — explained simply.
What is a Pointer?
A pointer is a variable that stores the memory address of another variable, instead of storing a value directly. This lets you work with data indirectly and efficiently pass large data structures without copying them.
int x = 10;
int *p = &x; // p stores address of x
printf("%d", *p); // dereference: prints 10Why Pointers Matter
Pointers let you build dynamic data structures (linked lists, trees), pass values by reference to functions, and manually manage memory using malloc/free — all essential for efficient, low-level programming.
Common Mistakes
Dereferencing an uninitialized pointer, forgetting to free allocated memory (memory leak), or using memory after it's freed (dangling pointer) are the most common bugs beginners face — always initialize and free carefully.
🌍 Real-World Use
Every linked list, tree, or graph implementation you'll write later in DSA depends on pointers. Databases and operating systems use pointer-based structures internally to manage memory pages and index records efficiently.
💡 Pro Tip
Always set a pointer to NULL right after freeing it: `free(p); p = NULL;`. This turns a silent, hard-to-find bug (using freed memory) into an immediate, obvious crash if you accidentally use it again.
🧪 Quick Self-Test
Check what you just learned — no pressure, just practice.
1. What does the & operator do in C?
2. What is a 'dangling pointer'?