Loading lessons...
Memory Address
Memory Address
Every variable lives at a specific location in memory. The address-of operator & placed in front of a variable name asks where that location is.
Getting an address
#include <iostream>
using namespace std;
string food = "Pizza";
cout << &food << endl;
The output is a number: the memory address where food actually lives.
Addresses are hexadecimal
Addresses print in hexadecimal (base 16), using the digits 0 to 9 and the letters A to F, usually with a 0x prefix:
cout << &food << endl; // 0x7ffe0000a1c
You never do math with that number; it is just a label for a spot in memory.
The address can change
Memory is shared with everything else running on the computer, so the same program can print one address in one run and a different address in another. That is normal.
& has two jobs
&variablein an expression is the address-of operator: it produces the address.&in a declaration -string &meal = food;- marks a reference.
The position tells you which meaning is in play.
TL;DR
&variablegives the memory address of that variable.- Addresses print in hexadecimal, often with a
0xprefix. - The address value differs between runs and machines.
&on a variable name is the address-of operator;&in a declaration is the reference marker.