error: cast from ‘char*’ to ‘int’ loses precision
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了error: cast from ‘char*’ to ‘int’ loses precision相关的知识,希望对你有一定的参考价值。
编译时出现错误:
error: cast from ‘char*’ to ‘int’ loses precision
原因:程序中存在
char* addrCom; addrCom= ......//赋值 if(-1 == (int)addrCom) //导致编译出错 { ...... }
上面是隐式转换,将其改为标准C++显示类型转换:static_cast<int>或这是reinterpret_cast<int>
依然是:
error: cast from ‘char*’ to ‘int’ loses precision
最后google了一下,找到了答案:http://stackoverflow.com/questions/1640423/error-cast-from-void-to-int-loses-precision
应该将(int)addrCom 改为(long)addrCom即可
解释:
因为我编译的系统为linux64位,其指针类型和long型大小相等(8B)而与int型4B,故会出现:loses precision。
故:先将char*-->long,然后从long到int自动隐式转换。
以上是关于error: cast from ‘char*’ to ‘int’ loses precision的主要内容,如果未能解决你的问题,请参考以下文章
warning: assignment makes pointer from integer without a cast
关于Linux下C编译错误(警告)cast from 'void*' to 'int' loses precision
error C2664: 'strcpy' : cannot convert parameter 2 from 'LPCTSTR' to 'const char
类型收窄 error C2397: conversion from ‘const int‘ to ‘char‘ requires a narrowing conversion