我试图找到数组元素的内存地址,但输出结果却是十六进制。如果您能告诉我如何将其转换为十进制输出,我将不胜感激。
这是代码,
#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;
}
最佳答案
在 C++11 中你可以转换为 uintptr_t
, 喜欢
cout << "Address of a[0] = " << static_cast<uintptr_t>(&a) << endl;
uintptr_t
类型专门设计用于表示任何可能的指针,因此该解决方案是可移植的。
我不知道 C++98/03 中的可移植解决方案,尽管通常转换为 size_t
(but not always)作品。相关:Converting a pointer into an integer .
编辑
查看http://en.cppreference.com/w/cpp/types/integer并且在 C++ 标准 18.4.1 header uintptr_t
是可选。我不知道是否存在未定义它的实现,以及为什么选择不定义它。欢迎任何评论。
关于c++ - 在 C++ 中将输出从十六进制转换为十进制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30496212/