Menu
Coddy logo textTech

Recap - Pointer Practice

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

challenge icon

Challenge

Easy

Create a program that demonstrates the complete pointer workflow by declaring a variable, creating a pointer to it, and modifying the variable's value through the pointer.

The following inputs 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 score and initialize it with the first input value
  2. Create a pointer named scorePtr that points to the score variable using the address-of operator
  3. Print the original value of score by accessing it directly (not through the pointer)
  4. Use the dereference operator to change the value of score to the second input value through the pointer
  5. Print the modified value of score by accessing it directly again
  6. Print the memory address stored in the pointer

Use the following exact output format:

Original score: [original value]
Modified score: [modified value]
Pointer address: [memory address]

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 an integer variable named 'score' and initialize it with initialValue
    // 2. Create a pointer named 'scorePtr' that points to the score variable
    // 3. Print the original value, modify through pointer, and print results
    
    return 0;
}

All lessons in Logic & Flow