【发布时间】:2019-10-23 03:18:50
【问题描述】:
d 是 double 类型的数据,p 是指向它的指针。当它们都分别显式地转换为 int 时,它会为指针 p 的转换给出以下错误。
:
从double* 转换为int 会丢失精度
#include<iostream>
using namespace std;
int main()
{
int i,j;
double d=3.5;
double* p=&d;
i=(int)d;
j=(int)p; // this line gives the error
cout<<i<<" "<<j;
return 0;
}
我预计会有错误
"从double* 到int 的转换无效。为什么不是这样?
另外,如果对指针p 这样做,为什么数据 d 的强制转换没有给出“丢失精度”错误?
【问题讨论】: