C++中 如何打印全局变量的地址(十六进制的格式)?

hjk_thinking 2008-10-24 06:30:27
代码如下
声明了一个名字一样的变量
分别位于自定义空间,全局变量,和局部变量
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<iostream>
#include <windows.h>

using namespace std;

namespace Jill
{
double bucket(double n)
{
cout<<"bucket = "<<n<<endl;
return n;
}
double fetch;
struct Hill
{
char c;
};
}

char fetch; //globle namespace;

int main()
{
using namespace Jill;
Hill Thill;

double water = bucket(2);
cout<<"water = "<<water<<endl;


double fetch;
cin>>fetch;
cout<<" 1 :fetch"<<fetch<<" , "<<&fetch<<endl;
printf("%p\n",&fetch);
cout<<DWORD(&fetch)<<endl;

cout<<"----------"<<endl;
cin>>::fetch;
cout<<"globle :2 fetch"<<::fetch<<endl;
cout<<&::fetch<<endl;//打印不出全局变量的地址
cout<<DWORD(&::fetch)<<endl; //打印出的是十进制的格式

printf("%p\n",&::fetch); //此方法打印全局变量的地址,但是用的 是C的方法


cout<<"--------"<<endl;
cin>>Jill::fetch;
cout<<"3 Jill: "<<Jill::fetch<<","<<&Jill::fetch<<endl;
return 0;
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...全文
564 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
帅得不敢出门 2008-10-24
  • 打赏
  • 举报
回复
要打印十六进制加个输出控制符 hex
机智的呆呆 2008-10-24
  • 打赏
  • 举报
回复

#include <iostream>
#include <windows.h>

using namespace std;

namespace Jill
{
double bucket(double n)
{
cout <<"bucket = " <<n <<endl;
return n;
}
double fetch;
struct Hill
{
char c;
};
}

char fetch; //globle namespace;

int main()
{
using namespace Jill;
Hill Thill;

double water = bucket(2);
cout <<"water = " <<water <<endl;


double fetch;
cin>>fetch;
cout <<" 1 :fetch" <<fetch <<" , " <<&fetch <<endl;
printf("%p\n",&fetch);
cout <<DWORD(&fetch) <<endl;

cout <<"----------" <<endl;
cin>>::fetch;
cout <<"globle :2 fetch" <<::fetch <<endl;
cout <<&::fetch <<endl;//打印不出全局变量的地址
cout <<"0x"<<hex<<(int)(&::fetch) <<endl; 这样写
ios::sync_with_stdio();
printf("%p\n",&::fetch); //此方法打印全局变量的地址,但是用的 是C的方法


cout <<"--------" <<endl;
cin>>Jill::fetch;
cout <<"3 Jill: " <<Jill::fetch <<"," <<&Jill::fetch <<endl;
system("pause");
return 0;
}

baihacker 2008-10-24
  • 打赏
  • 举报
回复
d:\>a
bucket = 2
water = 2
1
1 :fetch1 , 0x22ff60
0022FF60
22ff60
----------
2
globle :2 fetch2
0x446018
446018
00446018
--------
3
3 Jill: 3,0x446010
baihacker 2008-10-24
  • 打赏
  • 举报
回复
#include <iostream> 
#include <windows.h>

using namespace std;

namespace Jill
{
double bucket(double n)
{
cout <<"bucket = " <<n <<endl;
return n;
}
double fetch;
struct Hill
{
char c;
};
}

char fetch; //globle namespace;

int main()
{
using namespace Jill;
Hill Thill;

double water = bucket(2);
cout <<"water = " << hex <<water <<endl;


double fetch;
cin>>fetch;
cout <<" 1 :fetch" <<fetch <<" , " <<&fetch <<endl;
printf("%p\n",&fetch);
cout << hex << DWORD(&fetch) <<endl;

cout <<"----------" <<endl;
cin>>::fetch;
cout <<"globle :2 fetch" <<::fetch <<endl;
cout <<reinterpret_cast<int*>(&::fetch) <<endl;//打印不出全局变量的地址
cout <<hex << DWORD(&::fetch) <<endl; //打印出的是十进制的格式

printf("%p\n",&::fetch); //此方法打印全局变量的地址,但是用的 是C的方法


cout <<"--------" <<endl;
cin>>Jill::fetch;
cout <<"3 Jill: " <<Jill::fetch <<"," <<&Jill::fetch <<endl;
return 0;
}

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧