C++
#include <iostream>
using namespace std;
void doSomething(int y)
{
cout << y << " "<< & y << endl;
}
int main()
{
int x(0);
cout << x << " " << & x << endl;
doSomething(x);
return 0;
}
Python
def doSomething(y):
print(y, id(y))
x = 0
print(x, id(x))
doSomething(x)
I think their code should return same result however C ++ result is
0 00000016C3F5FB14
0 00000016C3F5FAF0
Python result is
0 1676853313744
0 1676853313744
i don't understand why variable's address isn't changed in Python while variable's address is changed in C++
doSomethingtovoid doSomething(int& y)you will see that now you get the same result as python.xis the variable declared in main, whileyis the function input parameter variable. So, indeed,xandyactually doesn't have the same address, then&xand&ydoes not points to the same location.xis not a global variable. It is a local variable in functionmain.