简体   繁体   中英

Converting an output from Hexadecimal to Decimal in c++

I was trying to find the memory address of an array elements but the output is turning out to be in Hexadecimal. I would appreciate if u could tell me how to convert it to a Decimal output.

Here is the Code,

#include<iostream>
using namespace std;

 int main()
  {
   int a[4];

   cout << "Address of a[0] = " << &a << endl;
   cout << "Address of a[1] = " << &a[1] << endl;
   cout << "Address of a[2] = " << &a[2] << endl;
   cout << "Address of a[3] = " << &a[3] << endl;

   cin.get();

   return 0;
  }

In C++11 you can cast to uintptr_t , like

cout << "Address of a[0] = " << static_cast<uintptr_t>(&a) << endl;

The type uintptr_t is specifically designed to represent any possible pointer, so the solution is portable.

I am not aware of a portable solution in C++98/03, although a cast to size_t often (but not always) works. Related: Converting a pointer into an integer .


EDIT

Looking at http://en.cppreference.com/w/cpp/types/integer and also at the C++ standard 18.4.1 Header <cstdint> synopsis [cstdint.syn] , it looks like uintptr_t is optional . I have no idea whether there is an implementation that does not define it, and why would one chose to not define it. Any comments are welcome.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM