Pointers in C Explained
A pointer in C is a variable that stores the memory address of another variable, rather than a value itself. If a normal variable is a box holding a value, a pointer is a note that says "the value is over there." Pointers are what make C powerful — and they're where beginners trip up most, so let's go slowly.
Addresses and the & operator
Every variable lives at some address in memory. The & (address-of) operator gives you that address:
#include <stdio.h>
int main(void) {
int x = 42;
printf("Value of x: %d\n", x);
printf("Address of x: %p\n", (void *)&x); /* %p prints an address */
return 0;
}
Declaring a pointer and dereferencing
A pointer's type says what it points to. The * in a declaration means "pointer to". To read the value at the address a pointer holds, use * again — this is dereferencing:
#include <stdio.h>
int main(void) {
int x = 42;
int *p = &x; /* p holds the address of x */
printf("p points to: %d\n", *p); /* dereference: reads 42 */
*p = 100; /* writes through the pointer... */
printf("x is now: %d\n", x); /* ...so x becomes 100 */
return 0;
}
The * is doing two different jobs: in the declaration int *p it marks p as a pointer; in the expression *p it dereferences to reach the value.
Want to learn this properly?
Join the waitlist for C Programming — beginner-friendly, project-first classes in Jalgaon.
Explore C ProgrammingWhy pointers matter: pass by pointer
C passes arguments by value, so a function gets a copy and can't change the caller's variable — unless you pass a pointer. This is how you write functions that modify their arguments:
#include <stdio.h>
/* Receives the ADDRESS, so it can change the caller's variable */
void doubleIt(int *n) {
*n = *n * 2; /* modify the value at that address */
}
int main(void) {
int value = 8;
doubleIt(&value); /* pass the address of value */
printf("%d\n", value); /* 16 */
return 0;
}
This is exactly how scanf("%d", &x) works — you give it the address so it can store your input into x.
NULL: a pointer to nothing
A pointer that points to no valid object should be set to NULL. Always check before dereferencing:
int *p = NULL;
if (p != NULL) {
printf("%d\n", *p); /* safe: only dereference when not NULL */
}
Dereferencing a NULL (or uninitialized) pointer is a crash waiting to happen.
Pointers and arrays
An array name acts like a pointer to its first element, which is why pointers and arrays are closely related. Pointers are also the foundation of dynamic memory allocation.
Common mistakes
- Dereferencing an uninitialized or NULL pointer — undefined behaviour, usually a crash. Always initialize pointers (to a valid address or
NULL). - Confusing
*roles:int *pdeclares;*pdereferences. They look the same but mean different things. - Returning the address of a local variable from a function — that variable disappears when the function returns; the pointer dangles.
- Type mismatch: an
int *should point to anint, not adouble. - Forgetting
&inscanf:scanf("%d", x)passes the value, not the address, and corrupts memory. Use&x.
FAQ
Why are pointers hard? They add one layer of indirection. Draw boxes-and-arrows on paper: a box for the value, an arrow from the pointer to it. It clicks with practice.
Is & always address-of? In an expression it's address-of; between two integers it's the bitwise AND operator. Context decides.
Continue at the hub C Programming, then Functions in C and Dynamic Memory in C.
Want to learn this properly? Join the waitlist for our C Programming course — taught in Jalgaon.
Want to learn this properly?
Join the waitlist for C Programming — beginner-friendly, project-first classes in Jalgaon.
Explore C ProgrammingFounder, Infoplanet
Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.
Related guides
Arrays in C
A beginner guide to arrays in C — declaring, initializing and indexing them, looping over elements, multidimensional arrays, and avoiding out-of-bounds errors.
Common C Errors & How to Fix Them
A troubleshooting guide to the most common C errors beginners face — from = vs ==, format mismatches and missing semicolons to segmentation faults — and how to fix each.
Dynamic Memory Allocation in C
A beginner guide to dynamic memory allocation in C — malloc, calloc, realloc and free, allocating at runtime, and preventing memory leaks.
