Loading...
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
A reference is an alternative name (alias) for an existing variable. It acts like a second label for the same memory location.
int x = 10; int &ref = x; // ref is an alias for x ref = 20; // x becomes 20!
Unlike pointers, references CANNOT be null. They must always refer to a valid object.
Once a reference is bound to a variable, it cannot be changed to refer to something else.
Pointers can be reassigned and be null; references are cleaner and safer but less flexible. Use references for parameters and pointers for dynamic data structures.