Is there a C ++ way to format addresses and pointers using iostream?

I have something like

unsigned x = 16; unsigned* p = &x; std::cout << std::hex << std::setw(16) << std::setfill('0') << x << std::endl; std::cout << std::hex << std::setw(16) << std::setfill('0') << p << std::endl; 

exit:

 0000000000000010 000x7fffc35ba784 

ostream :: operator <<is overloaded for this? I can write this correctly with C, but I was wondering if there is a way to do this with iostream.

+4
source share
1 answer

Use internal as follows:

 #include <iostream> #include <iomanip> int main() { unsigned x = 16; unsigned* p = &x; std::cout << std::hex << std::setw(16) << std::setfill('0') << x << std::endl; std::cout << std::hex << std::setw(16) << std::setfill('0') << p << std::endl; std::cout << std::internal << std::hex << std::setw(16) << std::setfill('0') << p << std::endl; } 

This gives:

 0000000000000010 000x7fffd123c1a4 0x007fffd123c1a4 
+7
source

Source: https://habr.com/ru/post/1501952/


All Articles