Recap - Pointer Practice
Part of the Logic & Flow section of Coddy's C++ journey — lesson 8 of 56.
Challenge
EasyCreate 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:
- Declare an integer variable named
scoreand initialize it with the first input value - Create a pointer named
scorePtrthat points to thescorevariable using the address-of operator - Print the original value of
scoreby accessing it directly (not through the pointer) - Use the dereference operator to change the value of
scoreto the second input value through the pointer - Print the modified value of
scoreby accessing it directly again - 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
1Pointers and Memory
What is a Pointer?Address-Of OperatorDereference OperatorNull PointersPointers and ArraysDynamic Memory with 'new'Freeing Memory with 'delete'Recap - Pointer Practice2Vectors (Dynamic Arrays)
Introducing std::vectorCreating a VectorAdding ElementsAccessing ElementsVector SizeIterating with a For LoopRange-Based For LoopRemoving ElementsRecap - Vector Operations5Project: Inventory Tool
Project SetupAdding and Updating Items