鱼C论坛

 找回密码
 立即注册
查看: 3802|回复: 2

reinterpret_cast<unsigned long>转换指针出错

[复制链接]
发表于 2020-8-7 09:58:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
在甲鱼哥哥的C++快速入门视频中看到reinterpret_cast<unsigned long>转换指针,输出地址,但是实践后出错,代码是一样的,如果不加reinterpret_cast<unsigned long>就可以输出,但是字符串的不是地址。提示错误在下面,为什么呢,请高手帮忙看下   感谢
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.         int intArr[]={1,2,3,4,5,6,7,8,9,10};
  6.         char charArr[]={'i','l','o','v','e','y','o','u'};
  7.         int *intPtr=intArr;
  8.         char *charPtr=charArr;
  9.         cout<<"整数数组输出;"<<endl;
  10.         for(int i=0;i< _countof(intArr);i++)
  11.         {
  12.                 cout<<*intPtr<<" at "<< reinterpret_cast<unsigned long>(intPtr)<<endl;
  13.                 intPtr++;
  14.         }

  15.         cout<<"字符数组输出;"<<endl;
  16.         for(int i=0;i< _countof(charArr);i++)
  17.         {
  18.                 //reinterpret_cast<unsigned long>()加起来出错
  19.                 cout<<*charPtr<<" at "<< reinterpret_cast<unsigned long>(charPtr)<<endl;
  20.                 charPtr++;
  21.         }
  22.         return 0;
  23. }
复制代码


提示错误:

[Error] cast from 'int*' to 'long unsigned int' loses precision [-fpermissive]
[Error] cast from 'char*' to 'long unsigned int' loses precision [-fpermissive]
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-8-7 17:21:19 | 显示全部楼层
可能是系统位数问题。
把 unsigned long 改为unsigned int 试试
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-1-14 18:07:16 | 显示全部楼层
把<unsigned long>换成<unsigned long long>试试看,我是这么运行成功的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-23 01:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表