马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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]
|