I'm trying to wrap my head around the concepts of pointers and lvalue references in C++. I get that when I create a pointer, it holds the address of another variable, like this: `int a {5}; int* b{ &a };`, where `b` now contains the address of `a`. Similarly, I can create a reference to a variable with `int& b{ a };`, which seems to give `b` a direct link to `a`. My confusion arises with lvalue references. I thought the '&' symbol indicates that a variable is a reference to another value, so why do pointers hold an address while references seem to create copies? Are pointers and references related in any way?
2 Answers
Think of references in C++ as just another name for the variable they reference, so there's no copy being made. When you change the reference, you also change the original variable. Conversely, a pointer is its own separate entity that stores the address and requires dereferencing to access the value. It's handy to think of them as different tools — references for simpler names, and pointers for explicit address manipulation.
Actually, a reference does hold the address of the variable it refers to, just like a pointer! However, you don't have to dereference a reference to access the value, which helps make code cleaner. References must be initialized when created and can't be null, making them safer for function arguments versus pointers.

Great way to put it! Definitely helps clarify the distinction.