1
Reference vs Pointer
C++
A reference is another name (alias) for an existing variable.
int x = 10;
int &r = x; // r is another name for x
r = 50;
cout << x; // 50
| Reference | Pointer |
|---|---|
| Must be initialised when declared | Can be declared without a value |
| Cannot be null | Can be nullptr |
| Cannot be changed to refer to another variable | Can point to another address later |
| No special syntax to access (r) | Needs * to access (*p) |