Menu
Coddy logo textTech

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 age

It'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 icon

Challenge

Easy

Write a C program that demonstrates the complete pointer workflow: declaration, address assignment, and dereferencing. Your program should:

  1. Declare an integer variable named value and initialize it with the value 75
  2. Declare a float variable named price and initialize it with the value 19.99
  3. Declare a pointer to an integer named value_ptr
  4. Declare a pointer to a float named price_ptr
  5. Use the address-of operator to assign the address of value to value_ptr
  6. Use the address-of operator to assign the address of price to price_ptr
  7. Use the dereference operator to print the value of value through value_ptr
  8. Use the dereference operator to print the value of price through price_ptr with exactly 2 decimal places

Your output should display the dereferenced values in the following format:

Value through pointer: 75
Price through pointer: 19.99

The 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 age

The asterisk (*) has two different meanings in C:

  • In declarations like int *ptr; - indicates that ptr is 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;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow