reinterpret_cast<unsigned long>转换指针出错
在甲鱼哥哥的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;
}
提示错误:
cast from 'int*' to 'long unsigned int' loses precision [-fpermissive]
cast from 'char*' to 'long unsigned int' loses precision [-fpermissive]
可能是系统位数问题。
把 unsigned long 改为unsigned int 试试 把<unsigned long>换成<unsigned long long>试试看,我是这么运行成功的
页:
[1]