Understanding Pointers in C
Ad
What is a Pointer?
A pointer is a variable that stores the memory address of another variable. Pointers are C's most powerful — and most feared — feature.
Basics
int x = 10;
int *p = &x; // p holds the address of x
printf("%d", *p); // 10 (dereference: value at address)
printf("%p", p); // the actual address
Why Pointers Matter
- Pass large data to functions by reference (no copying).
- Dynamic memory allocation.
- Build linked lists, trees, and other structures.
Pointers and Functions
void increment(int *n) { (*n)++; }
int a = 5;
increment(&a);
printf("%d", a); // 6 — modified the original
Dynamic Memory
int *arr = malloc(5 * sizeof(int));
arr[0] = 100;
free(arr); // always free what you malloc
FAQs
What is a null pointer?
A pointer set to NULL pointing to nothing. Always check before dereferencing. More in our C section.
What causes segmentation faults?
Accessing invalid memory — dereferencing NULL or freed pointers.
