鱼C论坛

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

reinterpret_cast<unsigned long>转换指针出错

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

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

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

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

        cout<<"字符数组输出;"<<endl;
        for(int i=0;i< _countof(charArr);i++)
        {
                //reinterpret_cast<unsigned long>()加起来出错
                cout<<*charPtr<<" at "<< reinterpret_cast<unsigned long>(charPtr)<<endl;
                charPtr++;
        }
        return 0;
}

提示错误:

[Error] cast from 'int*' to 'long unsigned int' loses precision [-fpermissive]
[Error] cast from 'char*' to 'long unsigned int' loses precision [-fpermissive]
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-8-7 17:21:19 | 显示全部楼层
可能是系统位数问题。
把 unsigned long 改为unsigned int 试试
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-1-14 18:07:16 | 显示全部楼层
把<unsigned long>换成<unsigned long long>试试看,我是这么运行成功的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-7 19:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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