{"metadata":{"orig_nbformat":4,"language_info":{"codemirror_mode":"text/x-c++src","file_extension":".cpp","mimetype":"text/x-c++src","name":"c++","version":"17"},"kernelspec":{"name":"xcpp17","display_name":"C++17","language":"C++17"}},"nbformat_minor":5,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# References","metadata":{},"id":"eb6a84a9-1411-4a9b-8cbb-c7ed29ecb9c0"},{"cell_type":"code","source":"#include <iostream>\n#include <memory>","metadata":{"trusted":true},"execution_count":7,"outputs":[],"id":"40de00aa-5b48-420d-8743-099cef5d316a"},{"cell_type":"code","source":"void check() {\n int iVal = 1;\n int* ptr2int = &iVal;\n int** ptr2ptr = &ptr2int;\n int& ref2int = iVal;\n // int&& ref2ref = ref2int; // this will not work\n \n std::cout << \"iVal stores a value of ................. \" << iVal << '\\n';\n std::cout << \"iVal resides in memory at address ...... \" << std::addressof( iVal ) << \"\\n\\n\";\n\n std::cout << \"ptr2int stores a value of .............. << \" << ptr2int << '\\n';\n std::cout << \"ptr2int resides in memory at address ... << \" << &ptr2int << \"\\n\\n\";\n\n std::cout << \"ptr2ptr stores a value of .............. << \" << ptr2ptr << '\\n';\n std::cout << \"ptr2ptr resides in memory at address ... << \" << &ptr2ptr << \"\\n\\n\";\n\n std::cout << \"ref2int stores a value of .............. << \" << ref2int << '\\n';\n std::cout << \"ref2int resides in memory at address ... << \" << &ref2int << std::endl;\n}","metadata":{"trusted":true},"execution_count":49,"outputs":[],"id":"0579d3c2-3f9d-4904-8762-f1d6cdb2581d"},{"cell_type":"code","source":"check()","metadata":{"trusted":true},"execution_count":50,"outputs":[{"name":"stdout","text":"iVal stores a value of ................. 1\niVal resides in memory at address ...... 0x7ffdf7adbfcc\n\nptr2int stores a value of .............. << 0x7ffdf7adbfcc\nptr2int resides in memory at address ... << 0x7ffdf7adbfc0\n\nptr2ptr stores a value of .............. << 0x7ffdf7adbfc0\nptr2ptr resides in memory at address ... << 0x7ffdf7adbfb8\n\nref2int stores a value of .............. << 1\nref2int resides in memory at address ... << 0x7ffdf7adbfcc\n","output_type":"stream"}],"id":"3a7034da-2d7f-4429-9853-ffd5e62eefcb"},{"cell_type":"markdown","source":"The last two lines clearly show the difference between a pointer and a reference.\nWhile the pointer is a variable with its own memory location, the reference is only an alias of sorts. It occupies the same place in memory and stores the same value as the object it is bound/initialised to.","metadata":{},"id":"410de220-647d-48f0-93e5-1789d2a72300"},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[],"id":"df3c18b4-c9b4-4823-8c71-adb4ec5c4593"}]}
The last two lines clearly show the difference between a pointer and a reference.
While the pointer is a variable with its own memory location, the reference is only an alias of sorts. It occupies the same place in memory and stores the same value as the object it is bound/initialised to.