Pointers and Addresses in C and C++
A pointer is just a variable that holds an address. Learn &, * and how to change a value through a pointer.
- Taking an address with &
- Reading and writing through * (dereference)
- Null pointers and C++ references
score: 75an intstored at address 0x7ffc10p: 0x7ffc10a pointerholds the address of score*pdereferencefollows the address back to score
What a pointer holds
The problem
Functions that change a caller's variable, arrays passed around without copying, linked lists and trees: all of them need a way to refer to data by where it lives in memory. In C that way is the pointer.
Think of it like this
A pointer is like a note with a house address written on it. The note is not the house, but by following the address you can go inside and change things. Copying the note does not copy the house; both notes lead to the same place.
int score = 70;🏠 The house itself&score📍 Its addressint *score_ptr = &score;📝 A note with the address written on it*score_ptr += 5;🚪 Go to the address and change what is insideA pointer is a variable that stores the memory address of another object; & takes an address and * follows a pointer to the object it points to.
Important points
&scoregives the address ofscore, andint *score_ptrstores it.*score_ptr += 5changes the value at that address, soscorebecomes 75.score_ptr == &scoreis true: both are the same address.- A pointer must point to a live object (or be NULL / nullptr) before you follow it.
When to use it
- Letting functions modify the caller's variables in C.
- Passing large arrays or structures without copying them.
- Building linked structures such as lists and trees.
When not to use it
- C++ code where a value can never be missing: a reference is simpler and cannot be null.
- Owning heap memory in C++: use
std::unique_ptror a container instead of a raw pointer.
C and C++ code examples
#include <stdio.h>
int main(void) {
int score = 70;
int *score_ptr = &score;
*score_ptr += 5;
printf("score=%d same-address=%s\n", score,
score_ptr == &score ? "yes" : "no");
return 0;
}
score=75 same-address=yes
#include <iostream>
int main() {
int score = 70;
int* score_ptr = &score;
*score_ptr += 5;
std::cout << "score=" << score << " same-address="
<< std::boolalpha << (score_ptr == &score) << '\n';
}
score=75 same-address=true
C and C++ comparison
score_ptr holds the address of score, not a copy of its value. Writing *score_ptr += 5 follows that address and changes score itself, so it prints 75. Comparing score_ptr with &score shows they are the same address.
Ownership and cleanup are conventions enforced by the programmer.
Containers and RAII types can encode ownership and cleanup in object lifetimes.
Practice exercises
Run both versions, then modify them to observe the different language guarantees.
- Test null and allocation-failure paths.
- Run with AddressSanitizer and UndefinedBehaviorSanitizer.
- Document whether every pointer owns or only observes its object.
Pointer syntax in C and C++
int x = 5; int *p = &x; | p holds the address of x |
*p | Dereference: the value at the address (5) |
*p = 10; | Change x through the pointer |
int *p = NULL; | A pointer that points nowhere (nullptr in C++) |
int *q = arr; | An array name becomes a pointer to its first element |
q + 1, q++ | Pointer arithmetic: move to the next element |
void swap(int *a, int *b) | Pass pointers so a function can change the caller's variables |
int **pp = &p; | A pointer to a pointer |
const int *p, int *const p | Pointer to a constant value, and a pointer that cannot move |
p->field | Access a struct member through a pointer |
int &ref = x; | C++: a reference, another name for x |
auto p = std::make_unique<int>(5); | C++: a smart pointer that frees its memory automatically |
Common mistakes with pointers
Using an uninitialized pointer
An uninitialized pointer holds a random address, so writing through it is undefined behavior. Point it at something first.
int *p;
*p = 5; /* undefined behavior */
int x;
int *p = &x;
*p = 5;
Dereferencing NULL
Reading or writing through a null pointer usually crashes the program. Check before you use it.
int *p = find_value(list, 42);
printf("%d\n", *p); /* crashes when not found */
int *p = find_value(list, 42);
if (p != NULL) printf("%d\n", *p);
Returning the address of a local variable
Local variables disappear when the function returns, so the pointer dangles. Return the value, or allocate memory.
int *make_number(void) {
int n = 42;
return &n; /* n no longer exists */
}
int *make_number(void) {
int *n = malloc(sizeof *n);
if (n) *n = 42;
return n; /* the caller must free(n) */
}
Forgetting to free memory
Every malloc needs a free (and every new a delete), or the program leaks memory. In C++, prefer smart pointers.
int *data = malloc(100 * sizeof *data);
/* ... use data, then return without free */
int *data = malloc(100 * sizeof *data);
/* ... use data ... */
free(data);
Frequently asked questions
What is a pointer in C?
A pointer is a variable that stores a memory address. &x gives the address of x, and *p gives the value stored at the address in p.
What is the difference between a pointer and a reference in C++?
A reference is another name for an existing variable: it must be initialized and cannot be changed to refer to something else or be null. A pointer can be null, can be reassigned and supports pointer arithmetic.
Why do we use pointers?
Pointers let a function change the caller's variables, avoid copying large data, work with arrays and dynamic memory, and build structures such as linked lists and trees.
What is the difference between NULL and nullptr?
Both mean the pointer points nowhere. NULL is a macro used in C, while nullptr is a C++11 keyword with its own type, so it avoids mix-ups with the integer 0.
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
All challenges