C Pointers Basics — Address, Dereference, Null
Learning Objectives
- Explain what a pointer variable stores
- Obtain and print the address of a variable
- Read and write values through a pointer
- Check for null before using a pointer safely
What a Pointer Is
A pointer is a variable whose value is a memory address. Instead of holding an integer or a character directly, it holds the location where such a value lives. Pointers connect names in your code to actual positions in memory and unlock functions that modify caller data, dynamic memory, and complex data structures.
Declaring a pointer requires the type of the data it will point to, an asterisk, and the pointer name. The type tells the compiler how to interpret the bytes at that address and how far to step during pointer arithmetic later.
int score = 42;
int *ptr = &score;
printf("%d\n", score);
printf("%d\n", *ptr);
The ampersand is the address-of operator. It yields the location of score. The asterisk on the left declares ptr as a pointer. The asterisk on the right of an existing pointer is the dereference operator, which fetches the value stored at the address.
Addresses and Values
It helps to separate three concepts: the variable name, the address it occupies, and the value stored there. You can print addresses in hexadecimal format to observe the relationship directly.
int count = 7;
int *p = &count;
printf("Value: %d\n", count);
printf("Address: %p\n", (void *)&count);
printf("Through pointer: %d\n", *p);
All three lines describe the same data from different angles. The cast to a generic pointer type in the address printout follows common practice and keeps the format specifier happy across platforms.
| Symbol | Name | Role |
|---|---|---|
| ampersand before variable | Address-of | Produces the variable location |
| type asterisk name | Pointer declaration | Creates a pointer variable |
| asterisk before pointer | Dereference | Accesses the pointed-to value |
Modifying Through a Pointer
Dereferencing on the left side of an assignment writes into the original object. This is how a function receives a pointer and updates a variable that lives in the caller's frame.
void addTen(int *value) {
*value = *value + 10;
}
int main(void) {
int total = 5;
addTen(&total);
printf("%d\n", total);
return 0;
}
The function receives the address of total, reads the current value, adds ten, and stores the result back. The caller then observes fifteen. Compare this with plain pass-by-value, where changes vanish when the function returns.
The Null Pointer
A null pointer intentionally points at nothing. Testing against null before dereferencing protects against crashes when a pointer might not yet reference a valid object. Uninitialized pointers are dangerous because they hold garbage addresses that may or may not be mapped.
int *maybe = NULL;
if (maybe != NULL) {
printf("%d\n", *maybe);
} else {
printf("Pointer is empty.\n");
}
Always initialize pointers when you declare them. Either assign the address of a real object immediately or set the pointer to null so checks behave predictably.
⚠️Dereferencing Null
Reading or writing through a null pointer is undefined behavior and typically crashes your program. Make null checks a habit before trusting a pointer, especially pointers received as arguments or returned from functions that can fail.
Common Pointer Mistakes
Beginners often forget the ampersand when passing a variable that a function must modify. Others confuse the two uses of the asterisk and try to dereference a value that is not a pointer at all. Read declarations from the inside out to keep roles straight: an int pointer named ptr means ptr holds the address of an int.
Another classic error is using a pointer after the object it referenced has gone out of scope or been freed. That produces a dangling pointer. Validity checks and clear ownership rules prevent most of these issues.
Key Takeaways
💡
- Pointers store addresses of other values in memory
- Address-of retrieves a location, dereference accesses the value there
- Passing addresses lets functions modify the caller's variables
- Initialize every pointer and prefer null over garbage addresses
- Never dereference a pointer without knowing it is valid
Practice Questions
- What is the difference between the address-of operator and the dereference operator?
- How does passing a pointer allow a function to change a caller's variable?
- Why should an uninitialized pointer be avoided even if it seems unused?
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type — Always start by categorizing the problem
- Incorrect formula application — Verify the formula applies to your specific case
- Unit errors — Always check units are consistent throughout
- Sign errors — Pay careful attention to positive and negative values
- Rounding too early — Keep full precision until the final answer
- Forgetting constraints — Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic — Build on the foundation laid here
- Next Topic — See how this concept extends to more advanced material
- Practice Problems — Additional exercises with solutions
- Video Explanation — Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions