Last active
June 3, 2017 17:18
-
-
Save vitalymak/ecac44c86285bbe598ec35e25de1099c to your computer and use it in GitHub Desktop.
The difference between a reference and a pointer C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Difference between a pointer and a reference in C++ | |
// http://cpp.sh/64ahf | |
#include <iostream> | |
int main() | |
{ | |
int a = 123; | |
int b = 456; | |
int & ra = a; // reference, also "int &ra = a; | |
ra = b; | |
int * p = &ra; // pointer, also "int *p = &ra; & means get address | |
std::cout << "a == ra == p* == " << *p << " - VALUE\n"; | |
std::cout << "&a == &ra == p == " << p << " - ADDRESS\n"; | |
std::cout << "&p = " << &p << " - ADDRESS OF THE POINTER\n"; | |
// a == ra == p* == 456 - VALUE | |
// &a == &ra == p == 0x78843187e654 - ADDRESS | |
// &p = 0x78843187e658 - ADDRESS OF THE POINTER | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment