LessonThis is a lesson restated in new words. Any figure in it is an example from the source, not this desk's record.
An integer variable holds a number. A pointer holds the address where a number of that type lives. If the pointer is set to the integer’s address, printing both addresses shows the same location, and writing a new number through the pointer changes the integer you started with. The lecture’s demonstration uses a spot price of 100, then 200 written through the pointer, and the original name then prints 200.
Why it matters
Functions, arrays, and large market objects are painful to copy on every call. Pointers are the mechanism C and C++ use to talk about a value that already exists somewhere in memory. The cost of that mechanism is confusion between three operations that look similar in source code: using the integer, asking for its address, and opening the address back up to see the integer. The lecture’s whole example is those three operations on one variable, timed so each print has an obvious right answer.
The address itself happened to be on the stack in the run they described. Stack versus heap is postponed. You do not need that distinction to see that two names can refer to one slot.
The idea in plain words
Declare an integer named spot price and set it to 100. Sending that name to the output stream prints 100. The type is int, so the value occupies an integer-sized slot. The ampersand operator asks for the address of that slot. Printing the address does not print 100. It prints wherever the compiler placed the variable. The lecture does not treat the particular digits of that address as meaningful. What matters is that the digits identify one slot.
A pointer to an integer is declared with the integer type and an asterisk in the declaration. The asterisk there means “this variable stores an address,” not “multiply.” Assign the pointer from the ampersand of the spot price. The pointer now contains that address. Print the address of the integer and print the pointer, and the lecture’s run showed the same address twice. Nothing was copied into a second integer. The pointer was aimed at the first one.
To get the integer back, put an asterisk in front of the pointer at the point of use. That asterisk is a dereference: follow the address and read the slot. The print then shows an integer again, the 100, rather than an address. Declaration-asterisk and use-asterisk are different jobs that share a character. The first creates a pointer variable. The second opens whatever address is currently in it.
The shared slot is the part people miss. Set the dereferenced pointer to 200 and then print the original name. The original prints 200, not 100. You did not assign the original name. You assigned the slot the original name labels, using a second way to name that slot. Change the original name to something else and a later dereference sees that new number for the same reason. There is one spot price.
How to use it
Say the three steps out loud when you write them. Create the integer and set a known value. Take its address with an ampersand and store that address in a pointer of the same base type. Print both addresses and require them to match before you dereference anything. Then dereference, require the known value, assign a second known value through the pointer, and require the original name to show the second value. If any print fails, stop. Later bugs in larger programs are the same three steps with worse names.
What usually goes wrong
Printing a pointer when you meant the integer, or the integer when you meant the pointer, swaps 100 with an address and looks like a random crash of the logic. The fix is the two-print check: addresses equal each other, and the dereference equals the integer.
Questions people ask
What does the asterisk mean in the declaration versus at use?
In the declaration it marks a pointer variable that will hold an address. At use, in front of an existing pointer, it reads or writes the object at that address. The lecture uses both, on the same integer.
What happened when the pointer was set to 200?
The integer slot became 200. Printing the original spot-price name showed 200, because the pointer and the name referred to one location.
Does this example say where the variable lives?
Only that, in the run they described, it was on the stack. The lecture postpones that topic. The result you can check without it is that the addresses matched and the assignment was shared.