Printing addresses with 0x notation

Viewed 508

Currently when I print an address of a variable, cout << &a, its displayed as a simple hexadecimal number, let's say: 008FFBB8.

How can I print an address with the 0x notation? e.g. 0x6FFDF4.

I tried doing:

cout << hex << &a;

but it doesn't seem to work.

3 Answers

A simple approach

cout << "0x" << &a;

That said, given that other systems do inlude 0x in &a, in order to make it portable, you should make cout << "0x" conditional based on predefined macro that detects the systems where 0x isn't included.

A portable solution is to insert into a string stream first, then check whether the prefix was added, and concatenate if it wasn't. this also prevents possibility of 0x and the address being separated by concurrent output.


I just wanted to know why Visual Studio is not displaying this notation by default,

Because the authors of their standard library chose to do so.

so I don't have to manually do it.

I'm not sure if there is a way to change their implementation. Before asking how, I recommend considering why you think that you have to do it.

There are many different solutions, the first two that came into my mind are the following:

#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>

int main()
{

  std::cout << std::hex
            << "showbase: " << std::showbase<< 42 << '\n'
            << "noshowbase: " << std::noshowbase << 42 << '\n';


  //2nd solution
  std::string a{"008FFBB8"};
  std::cout << "Given: " << a << '\n';
  a.erase(0,2);
  a = "0x"+a;
  std::cout << "Edited: "<< a <<'\n';


  //3rd string to int so you can use standard arithmetics
  int b = std::stoul(a, nullptr, 16);
  std::cout << "Represented as int: " << b << '\n';



  std::cin.get();
}

Edit:

This is how it is printed after compilation with GNU GCC(g++) compiler. enter image description here

The reason visual studio isn't printing it as shown on the screenshot is because visual studio tend not to use GNU GCC but its Microsoft compiler MSVC.(There are other things MSVC isn't doing as you may expect btw.) But good news: you can make it! or here:)

It is just how hex is presented in your configuration with "MSVC" I hope that answers your question, else leave a comment :)

Taking inspiration ;) from MSDN and trying it on VS 2019:

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    ios state(nullptr);

    int a;
    state.copyfmt(cout); // save current formatting
    cout << "In hex: 0x" // now load up a bunch of formatting modifiers
        << hex
        << uppercase
        << setw(8)
        << setfill('0')
        << &a            // the actual value we wanted to print out
        << endl;
    cout.copyfmt(state); // restore previous formatting
}

Output:

In hex: 0x00AFFB44

And as for why, well...you could raise an issue on MSVC and claim parity with all other compilers and platforms if you have the data. But given that they themselves manually slipped in the 0x I'm guessing they are already comfortable with the behavior.

Related