The Dereference Operator (*)
Part of the Logic & Flow section of Coddy's C journey — lesson 4 of 63.
Now that you know how to store addresses in pointers, you need to learn how to access the actual value stored at that memory address. The dereference operator (*) allows you to "follow" the pointer to get the value it points to.
When you place the * operator in front of a pointer variable, it accesses the value stored at the memory address the pointer is holding. Think of it as saying "give me what's inside the room at this address."
int age = 25;
int *ptr = &age;
printf("%d", *ptr); // Prints 25 - the value of ageIt's important to understand that the asterisk (*) has two different meanings in C. When declaring a pointer like int *ptr;, the asterisk indicates that ptr is a pointer. When used with an existing pointer like *ptr, it dereferences the pointer to access the value it points to.
The dereference operator is essential because it completes the pointer cycle: you can store an address in a pointer with &, and then retrieve the value at that address with *. This allows you to indirectly access and modify variables through their memory addresses.
Challenge
EasyWrite a C program that demonstrates the complete pointer workflow: declaration, address assignment, and dereferencing. Your program should:
- Declare an integer variable named
valueand initialize it with the value 75 - Declare a float variable named
priceand initialize it with the value 19.99 - Declare a pointer to an integer named
value_ptr - Declare a pointer to a float named
price_ptr - Use the address-of operator to assign the address of
valuetovalue_ptr - Use the address-of operator to assign the address of
pricetoprice_ptr - Use the dereference operator to print the value of
valuethroughvalue_ptr - Use the dereference operator to print the value of
pricethroughprice_ptrwith exactly 2 decimal places
Your output should display the dereferenced values in the following format:
Value through pointer: 75
Price through pointer: 19.99The challenge tests your understanding of how the dereference operator retrieves the original variable's value by following the pointer's stored address.
Cheat sheet
The dereference operator (*) allows you to access the value stored at the memory address a pointer is holding.
int age = 25;
int *ptr = &age;
printf("%d", *ptr); // Prints 25 - the value of ageThe asterisk (*) has two different meanings in C:
- In declarations like
int *ptr;- indicates thatptris a pointer - With existing pointers like
*ptr- dereferences the pointer to access the value it points to
The dereference operator completes the pointer cycle: store an address with &, then retrieve the value at that address with *.
Try it yourself
#include <stdio.h>
int main() {
// TODO: Write your code here
// 1. Declare and initialize the variables
// 2. Declare the pointers
// 3. Assign addresses to pointers
// 4. Print the dereferenced values
return 0;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Pointers Fundamentals
What is a Pointer?Declaring PointersThe Address-Of Operator (&)The Dereference Operator (*)NULL PointersRecap: Pointer Basics4Project: Simple Text Utility
Project OverviewCounting Characters2Pointers and Arrays
Array Names as PointersArray Elements - PointersPointer ArithmeticComparing PointersRecap: Pointer Array Traversal5Pointers and Functions
Pass-by-ValuePassing Pointers to FunctionsModifying Vars via PointersA Classic Example: SwapPassing Arrays to FunctionsRecap: Function Pointer Args8Structs and Pointers
Pointers to StructsThe Arrow Operator (->)Passing Structs by ValuePassing Struct PointersDynamic Allocation of StructsRecap: Modifying Struct - Ptr11Final Recap Challenges
Recap: Dynamic String ConcatRecap: Array of StructsRecap: Word Frequency Counter3Character Arrays and Strings
Strings as char ArraysThe Null TerminatorString Input with scanfUsing strlen()Using strcpy()Using strcat()Using strcmp()Recap: Basic String Functions