1
int x = 99;
printf("&x = %p\n", &x);
printf("&x = %d\n", &x);
std::cout << &x << std::endl;
std::cout << std::dec << &x << std::endl; // problematic line (5)
std::cout << std::dec << 0x0D << std::endl;

The output is:

&x = 0x7ffe5f1d0434
&x = 1595737140 // I don't think this is correct.
0x7ffe5f1d0434
0x7ffe5f1d0434
13 // decimal of 0x0D

I don't understand why fifth line above does not output decimal representation of the address of variable x. My usage of std::dec should be correct, because it works for 0x0D. So, I suspect &x might not be a hexadecimal number as I think it is. But, I am not sure, so wanted to ask.

Thanks in advance.

muyustan
  • 1,555
  • 1
  • 11
  • 23
  • `0x0D` is an integer value. A pointer is not. Try `static_cast(&x)`. – François Andrieux Apr 06 '20 at 18:44
  • `0x7ffe5f1d0434` represents 6 bytes. If you discard the two most significant bytes and convert `0x5f1d0434` to decimal, you'll see it matches up with your output of `1595737140`. That suggests to me that the output is being truncated to a 32-bit integer. – John H Apr 06 '20 at 18:44
  • @JohnH probably, it gave a warning though : `c.cpp:9:27: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int*’ [-Wformat=] printf("&x = %d\n", &x);` – muyustan Apr 06 '20 at 18:45
  • Good warning. They both should be `static_cast` to `uintptr_t`. – JohnFilleau Apr 06 '20 at 18:56

0 Answers0