c++的一个程序 不知道错在哪里?请大哥指导下,在线等答案。。。。

huanmietd 2009-04-25 08:45:07
源程序如下:
#include<iostream>
using namespace std;

int main()
{
float f=19.2F;
unsigned int* pa = reinterpret_cast<int*>(&f);
for(int i=31;i>=0;i--)
{
cout<<(*pa>>i & 1)<<(i==31||i==23 ? "-":" ");
}
cout<<endl;
return 0;
}

编译错误信息:
D:\Microsoft Visual Studio\MyProjects\test\test.cpp(7) : error C2440: 'initializing' : cannot convert from 'int *' to 'unsigned int *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
执行 cl.exe 时出错.

test.obj - 1 error(s), 0 warning(s)
...全文
148 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
deltamaster 2009-04-25
  • 打赏
  • 举报
回复
reinterpret_cast还是比较危险的。
能不用还是尽量不用的好。
huanmietd 2009-04-25
  • 打赏
  • 举报
回复
谢谢各位的帮助。。。。谢谢。。。
  • 打赏
  • 举报
回复

int main()
{
float f=19.2F;
unsigned int* pa = reinterpret_cast <unsigned int*>(&f); //楼主你就不会改一下啊,现在可以了
for(int i=31;i>=0;i--)
{
cout <<(*pa>>i & 1) <<(i==31||i==23 ? "-":" ");
}
cout <<endl;
return 0;
}
WaistCoat20 2009-04-25
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

int main()
{
float f=19.2F;
unsigned int* pa = reinterpret_cast <unsigned int*>(&f);
for(int i=31;i>=0;i--)
{
cout < <(*pa>>i & 1) < <(i==31||i==23 ? "-":" ");
}
cout <<endl;

return 0;
}
jackyjkchen 2009-04-25
  • 打赏
  • 举报
回复
无法从“int *”转换为“unsigned int *”

unsigned int* pa = reinterpret_cast <int*>(&f);

修改下类型
「已注销」 2009-04-25
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

int main()
{
float f = 19.2F;
unsigned int* pa = reinterpret_cast <unsigned int*>(&f);
for (int i = 31;i >= 0;i--)
{
cout << (*pa >> i & 1) << (i == 31 || i == 23 ? "-" : " ");
}
cout << endl;
return 0;
}
「已注销」 2009-04-25
  • 打赏
  • 举报
回复
reinterpret_cast <int*>(&f); 改成:reinterpret_cast <unsigned int*>(&f);

65,211

社区成员

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

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