2

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.

Hexed
  • 45
  • 5
  • 4
    https://en.cppreference.com/w/cpp/io/manip/showbase – Retired Ninja Mar 25 '21 at 14:59
  • Does this answer your question? [Correct format specifier to print pointer or address?](https://stackoverflow.com/questions/9053658/correct-format-specifier-to-print-pointer-or-address) – ma1169 Mar 25 '21 at 15:21
  • @RetiredNinja I tried doing this, and although it works with just numbers, it doesn't seem to work with addresses. cout << hex << "Address: " << showbase << p << endl << "Value: " << showbase << *p << endl; and the result is: Address: 0057F8C8 Value: 0x2 – Hexed Mar 25 '21 at 15:41
  • 2
    @ma1169 The post you mentioned as duplicate is for C language, not C++ – Damien Mar 25 '21 at 15:45
  • Also I would like to add that while the 0x notation doesn't display in Visual Studio, it is displaying by default on online cpp compilers. – Hexed Mar 25 '21 at 15:47
  • I confirm that everything is fine (0x...) with gcc 10.2.0 – Damien Mar 25 '21 at 15:53
  • @Damien up to my knowledge there's 2 ways to do it, in C++ concatenate 0x with std::hex or do it in C style and using C in C++ is not uncommon – ma1169 Mar 25 '21 at 15:55
  • @ma1169 up to my knowledge too. This is not the problem. The problem is that you use the same wording "Does this answer ... " that is usually used to indicate a duplicate. The post you mentioned is related, but is not a duplicate. – Damien Mar 25 '21 at 16:01

3 Answers3

2

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.

eerorika
  • 232,697
  • 12
  • 197
  • 326
2

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 :)

Ingo Mi
  • 999
  • 12
  • 26
  • Thanks for answering. The first solution did actually work with numbers but it didn't affect the address. It displayed as it was before. even after adding the hex function. As for the second solution, I just wanted to know why Visual Studio is not displaying this notation by default, so I don't have to manually do it. – Hexed Mar 25 '21 at 16:06
  • @Hexed I added my answer :) – Ingo Mi Mar 25 '21 at 16:16
  • @Hexed That is awesome, you are welcome :) Please don't forget to mark one answer as "accepted" to flush your question out of the stackoverflow queue. – Ingo Mi Mar 25 '21 at 22:57
1

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.

Zoso
  • 3,273
  • 1
  • 16
  • 27