= C++ Pointers and References = <> ---- == Pointers == C++ has the same memory management capabilities as C, including working with '''pointers'''. {{{ #include int main() { int a = 5; int *ptr = a; std::cout << ptr < '\n'; std::cout << *ptr < '\n'; return 0; } }}} This prints a hexadecimal address like `0x7ffe1a8207dc` and `5`. ---- == References == C++ adds the concept of '''references''', which are essentially pointers that cannot be null. In fact references are often internally implemented as pointers. {{{ #include int main() { int a = 5; int &ref = a; std::cout << ref < '\n'; return 0; } }}} This prints `5`. ---- == Pointers vs. References == Generally it is recommended to use references unless they are unavailable, or unless a null address is within a function's domain. References are ''not'' objects. See [[https://en.cppreference.com/w/cpp/language/reference|here]]: "there are no arrays of references, no pointers to references, and no references to references". {{{ int& a[3]; // error int&* p; // error int& &r; // error }}} Some functions are written to properly handle the case of a null pointer. This is perfectly valid and negates the benefits of references. ---- CategoryRicottone