Module 6 · Pointers and Memory — topic 1 of 5
Pointers
A pointer is a variable that holds the address of another object. Learn the & and * operators, pointer types, NULL, and how to read pointer declarations.
Learning objectives
- Declare pointers and obtain addresses with &
- Read and write through pointers with *
- Use NULL and recognise dangling and wild pointers
Definition
A pointer is an object whose value is the address of another object or function. The unary&operator yields the address of its operand; the unary*operator (indirection) accesses the object a pointer points to.
Introduction
Every variable lives at some address in memory — a number identifying where its bytes start. A pointer is simply a variable that stores such an address. With it, code can reach and change data that lives somewhere else.
Pointers are why C can pass large structures to functions without copying them, build linked lists and trees, work with dynamically allocated memory, and talk to hardware at fixed addresses.
Why it matters
Pointers are the single most important — and most feared — C topic. They are required for dynamic memory, strings, arrays in functions, data structures and file handling.
Interviews probe pointer understanding with output questions; real systems fail because of null and dangling pointers. Learning the rules precisely pays off twice.
Detailed explanation
int *p; declares p as a pointer to int. The * belongs to the declarator: int *p, q; makes p a pointer and q a plain int.
p = &x; stores the address of x in p. Now *p is x: reading *p gives x's value and *p = 5; changes x.
The pointer's type tells the compiler what it points to, so *p reads the right number of bytes and pointer arithmetic steps the right distance. void * is a generic pointer that can hold any object address but cannot be dereferenced without a cast.
NULL is a null pointer constant meaning “points to nothing”. Dereferencing a null pointer is undefined behaviour (usually a crash). Always initialise pointers — to a valid address or to NULL — and check before dereferencing when a pointer may be null.
A dangling pointer points to an object that no longer exists (a returned local, freed memory). A wild pointer was never initialised. Both are undefined behaviour to dereference.
Syntax
*in a declaration means “pointer to”; in an expression it means “the object pointed to”.- Print addresses with
%pand a cast tovoid *. - A pointer's own size (often 8 bytes) is independent of what it points to.
Code examples
Address-of and dereference
x = 10, *p = 10 after *p = 25, x = 25 p == &x ? 1
Pointer to pointer
99 99 99
More examples
Pointers re-pointed and checked
*p = 3 a = 3, b = 10 maybe is NULL, not dereferenced through void*: 2.5 sizeof(int*) == sizeof(double*) ? 1
Step-by-step explanation
pfirst points toa, so*pis 3.- Assigning
p = &bchanges which objectprefers to;ais untouched. *p += 2updatesbfrom 8 to 10 through the pointer.- The null check prevents dereferencing
maybe. - A
void *can hold the address ofd; converting back todouble *lets us read it. On common platforms all object pointers have the same size.
Expected output
Running “Pointers re-pointed and checked” prints exactly:
Practical applications
- Linked lists, trees and graphs link nodes with pointers.
- Operating systems pass buffers to drivers as pointers.
scanf("%d", &n)works because it receives a pointer ton.
Best practices
- Initialise every pointer, to a valid address or
NULL. - Check pointers that may be null before dereferencing.
- Set pointers to
NULLafter the object they point to is freed or goes out of scope.
Common mistakes
Dereferencing an uninitialised pointer
Avoid
Prefer
p holds a garbage address; writing through it corrupts memory or crashes.
Misreading a multi-pointer declaration
Avoid
Prefer
The * applies to each declarator separately. Write one declaration per line or repeat the *.
Important notes
- C23 introduces
nullptras a typed null pointer constant;NULLremains valid. - Comparing or subtracting pointers is only defined for pointers into the same array (or one past its end).
Key takeaways
&xgives an address;*paccesses the object at that address.- A pointer's type determines what
*preads. - Never dereference null, wild or dangling pointers.
Try it yourself
- 1Write code that uses a pointer to read an integer with scanf and doubles it through the pointer.
- 2Declare
int x = 5; int *p = &x; int **q = &p;and print x through p and through q.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
9 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerIn `int *p, q;`, what is the type of q?
- BeginnerWhat does the unary `&` operator produce?