Skip to content

Instantly share code, notes, and snippets.

@sprytnyk
Created July 29, 2019 09:39
Show Gist options
  • Save sprytnyk/713c4ed1c781aee8cbbf748306765c4f to your computer and use it in GitHub Desktop.
Save sprytnyk/713c4ed1c781aee8cbbf748306765c4f to your computer and use it in GitHub Desktop.
Pointers & references in Python & CPP (rough analogues).
#include <iostream>
using namespace std;
int main() {
// References in work.
int x(0);
int &ref = x;
x = 10;
cout << x << endl;
cout << ref << endl;
cout << &ref << endl;
/*
Python analogue:
i = 1
j = i
hex(id(i)) == j hex(id(j))
i & j have the same memory addresses (share it)
*/
// Pointers in work.
int y(10);
int *point = &y;
cout << endl;
cout << y << endl;
cout << point << endl;
y = 20;
cout << y << endl;
cout << point << endl;
/*
Python analogue:
i = 1
j = hex(id(i))
hex(id(i)) != hex(id(j))
i & j memory addresses aren't coincide but j holds memory address of i
*/
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment