Menu
Coddy logo textTech

Dereference Operator

Part of the Logic & Flow section of Coddy's C++ journey — lesson 3 of 56.

Now that you can get a pointer to a variable, you need to learn how to access and modify the value at that memory address. This is where the dereference operator (*) becomes essential.

The dereference operator * is used to access the value stored at the memory address that a pointer is pointing to. When you place * before a pointer variable, it "follows" the address and gives you the actual value stored there.

int number = 42;
int* ptr = &number;  // ptr stores the address of number
int value = *ptr;    // value now contains 42

You can also use the dereference operator to modify the original variable through the pointer. When you assign a new value to *ptr, you're actually changing the value stored at that memory location:

*ptr = 100;  // Changes number to 100
// number is now 100, even though we modified it through ptr

This indirect access is what makes pointers so powerful - you can read and modify variables from anywhere in your program as long as you have a pointer to them.

challenge icon

Challenge

Easy

Create a program that demonstrates the dereference operator by modifying a variable's value through a pointer.

The following input will be provided:

  • An integer representing the initial value for a variable
  • An integer representing the new value to assign through the pointer

Your program should:

  1. Declare an integer variable named temperature and initialize it with the first input value
  2. Create a pointer named tempPtr that points to the temperature variable
  3. Print the original value by dereferencing the pointer
  4. Use the dereference operator to change the value of temperature to the second input value through the pointer
  5. Print the new value by dereferencing the pointer again

Use the following exact output format:

Original value: [original value]
New value: [new value]

Cheat sheet

The dereference operator * is used to access the value stored at the memory address that a pointer is pointing to:

int number = 42;
int* ptr = &number;  // ptr stores the address of number
int value = *ptr;    // value now contains 42

You can also use the dereference operator to modify the original variable through the pointer:

*ptr = 100;  // Changes number to 100
// number is now 100, even though we modified it through ptr

This indirect access allows you to read and modify variables from anywhere in your program as long as you have a pointer to them.

Try it yourself

#include <iostream>
using namespace std;

int main() {
    // Read input values
    int initialValue, newValue;
    cin >> initialValue;
    cin >> newValue;
    
    // TODO: Write your code below
    // 1. Declare temperature variable and initialize with initialValue
    // 2. Create tempPtr pointer pointing to temperature
    // 3. Print original value using pointer dereference
    // 4. Change temperature value through pointer using newValue
    // 5. Print new value using pointer dereference
    
    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