On my computer. When I test the code:
int main()
{
int i=123;
return 0;
}
using
g++ -g test.cpp -o test
I found when I enter:
print &i output: 0x7fffffffe18c
print sizeof(&i) output: 8
I was confused, the address of i is 6 byte, why sizeof(&i)==8
?
Thanks a lot
When you do this, you are getting the address of i
The output show the address number that the variable i is stored, but printf will remove the leading zero, so you could only see 0x7fffffffe18c instead of 0x00007fffffffe18c, you could use a debugger to verify it
When you call sizeof(&i)
You getting 8 bytes, because you are getting the sizeof the address and not the variable i size, if you want to get the variable size just do