A reference acts as another name for one object. A pointer is a value that can store an object's address: information that lets the program locate that object. You do not need to print or calculate the numeric address to use it.
Three spellings matter. In a declaration, int* chosen says that chosen is a pointer to an integer. In an expression, &apples obtains the address of apples. In an expression, *chosen follows the pointer to the integer it identifies; this is called dereferencing.
#include <iostream>
int main() {
int apples = 6;
int* chosen = &apples;
*chosen += 1;
std::cout << apples << '\n';
}
This prints 7. The pointer does not contain a separate copy of six. It identifies the existing integer, and writing through *chosen updates that integer. Notice how context changes the symbols: & declares a reference beside a type, but takes an address before a variable expression. Similarly, * declares a pointer beside a type and dereferences a pointer expression.
Unlike a reference, an ordinary pointer can be assigned a different address later. Writing chosen = &another changes which object it points to. Writing *chosen = 10 changes the currently selected object's value. Those are not interchangeable operations.
Only dereference a pointer that identifies a live object of the appropriate type. This lesson initializes the pointer immediately and keeps both possible target integers alive for every use. The next lesson introduces the explicit “no object” state.
For the first task, change the address stored when selected is initialized. You are choosing a bin, not changing either bin's count. For the second, leave reservePointer pointing at reserve and add the update through *reservePointer. Keeping these edits separate helps you see which operation changes an address and which changes an integer.
Compare this diagram with the reference diagram: the pointer needs its own stored value, which identifies the target object.
Unlike the reference in the earlier lesson, p is a separate object storing an address. Writing through *p changes score while leaving that address in p unchanged.
selected with the address of largeBin instead of smallBin. Expect Selected units: 12.*reservePointer, without assigning directly to reserve. Expect Reserve: 9.Edit main.cpp, then select Run to inspect the Console. Select Submit to check your completed tasks.