Pointers and the & and * Operators
What a pointer actually stores, and how & and * relate a pointer to the variable it points to.
What you'll learn
- Explain that a pointer variable stores a memory address
- Use & to get a variable's address and * to dereference a pointer back to its value
- Predict that modifying *pointer changes the original variable it points to
Explanation
A pointer is a variable whose value is a memory address rather than an ordinary value like an int or a char. You declare one with an asterisk before its name: int *agePtr; declares agePtr as "a pointer to an int." The pointer itself takes up a fixed amount of memory (typically 4 or 8 bytes depending on the system) regardless of what type it points to.
Two operators connect a pointer to the variable it refers to. The address-of operator, &, gets a variable's memory address: int *agePtr = &age; makes agePtr point to age. The dereference operator, *, goes the other direction -- given a pointer, *agePtr accesses (reads or writes) the value stored at that address. Note that * here means two different things depending on context: in a declaration (int *agePtr), it marks the variable as a pointer type; in an expression (*agePtr = 26;), it dereferences an existing pointer.
This is the mechanism C uses to let a function modify a caller's variable: instead of passing the variable itself (which would just copy it, per pass-by-value), you pass its address, and the function dereferences the pointer to reach the real variable. Writing through *agePtr = 26; genuinely changes age itself, not a copy -- this is the foundation of everything pointers are used for in C, from function output parameters to dynamic memory to arrays.
Guided lab
Fill in the blank: writing through a pointer
Fill in the missing operator so the assignment writes through the pointer, then predict the output.
#include <stdio.h>
int main(void) {
int age = 25;
int *agePtr = &age;
printf("Before: %d\n", age);
____agePtr = 26;
printf("After: %d\n", age);
return 0;
}Stuck? Get a hint.
Common mistakes
- Confusing the two meanings of `*` -- `int *p` declares a pointer type, while `*p` in an expression dereferences an existing pointer to reach its target's value.
- Dereferencing a pointer that was never initialized to point anywhere valid, reading or writing an essentially random memory address.
- Forgetting `&` when trying to get a variable's address, and instead accidentally using the variable's value where an address was expected.
Knowledge check
Takeaway
`&variable` gets an address; `*pointer` reads or writes the value at that address -- writing through a dereferenced pointer changes the original variable, not a copy.
Summary
A pointer stores a memory address; `&` obtains one from a variable, and `*` dereferences a pointer to reach the value it points to, letting code modify the original variable.
References
Your notes
Notes save automatically.
Finished this lesson?
Mark it complete to track your progress and schedule a future review.
AI tutor
The optional AI tutor isn't enabled in this deployment. All lessons, exercises, quizzes, and search work fully without it.