03 Dev [Error] cast from ‘int*‘ to ‘int‘ loses precision [-fpermissive]

 在学习黑马基础课的案例时
由于编译器的不同,产生了报错

#include<iostream>
using namespace std;

int main()
{// 计算数组长度
int arr[10]={1,2,3,4,5,6,7,8,9,10 };
cout<<"整个数组占用内存空间为:"<<sizeof(arr)<< endl;
cout<<"每个元素占用内存空间为:"<<sizeof(arr[0])<< endl;
cout<<"数组中元素个数为:"<<sizeof(arr)/sizeof(arr[0])<<endl ;
//2、可以通过数组名查看数组首地址
cout<<"数组首地址为:"<<(int)arr<<endl;
cout<<"数组中第一个元素地址为:"<<(int)&arr[0]<< endl;
system("pause");
return 0;
}

因为基于Linux内核的64位系统上指针类型占用8个字节,而int类型占用4个字节,会出现loses precision 错误信息。

可以先将int* 转成long类型 long类型可以隐式类型转换到int类型。直接修改为long long即可

  #include<iostream>
using namespace std;

int main()
{
//
int arr[10]={1,2,3,4,5,6,7,8,9,10 };
cout<<"整个数组占用内存空间为:"<<sizeof(arr)<< endl;
cout<<"每个元素占用内存空间为:"<<sizeof(arr[0])<< endl;
cout<<"数组中元素个数为:"<<sizeof(arr)/sizeof(arr[0])<<endl ;
//2、可以通过数组名查看数组首地址
cout<<"数组首地址为:"<<(long long)arr<<endl;
cout<<"数组中第一个元素地址为:"<<(long long)&arr[0]<< endl;

system("pause");

return 0;
}

运行结果如下:

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Welcome to Beginning C++17. This is a revised and updated version of Ivor Horton’s original book called Beginning ANSI C++. The C++ language has been extended and improved considerably since then, so much so that it was no longer possible to squeeze detailed explanations of all of C++ into a single book. This tutorial will teach the essentials of the C++ language and Standard Library features, which will be more than enough for you to write your own C++ applications. With the knowledge from this book, you should have no difficulty in extending the depth and scope of your C++ expertise. We have assumed no prior programming knowledge. If you are keen to learn and have an aptitude for thinking logically, getting a grip on C++ will be easier than you might imagine. By developing C++ skills, you’ll be learning a language that is already used by millions and that provides the capability for application development in just about any context. C++ is very powerful. Arguably, it’s more powerful than most programming languages. So, yes, like with any powerful tool you can wield some considerable damage if you use it without proper training. We often compare C++ to a Swiss Army knife: age-old, trusted, incredibly versatile, yet potentially mind-boggling and full of pointy things that could really hurt you. Once someone clearly explains to you what all the different tools are meant for, however, and teaches you some elementary knife safety rules, then you’ll never have to look for another pocketknife again. C++ does not need to be dangerous or difficult at all either. C++ today is much more accessible than many people assume. The language has come a long way since its conception nearly 40 years ago. In essence, we have learned how to wield all its mighty blades and tools in the safest and most effective way possible. And, more importantly perhaps, the C++ language and its Standard Library have evolved accordingly to facilitate this. The past decade in particular has seen the ris

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

控制Mpc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值