Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vitalymak/ecac44c86285bbe598ec35e25de1099c to your computer and use it in GitHub Desktop.
Save vitalymak/ecac44c86285bbe598ec35e25de1099c to your computer and use it in GitHub Desktop.
The difference between a reference and a pointer C++
// 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