warning: cast from ‘void*’ to ‘int’ loses precision [-fpermissive] 错误分析

阅读 118

2022-04-29

转载:warning: cast from ‘void*’ to ‘int’ loses precision [-fpermissive] 错误分析_虚渊玄的博客-CSDN博客

这种错误一般产生在linux 64位机上

因为linux64 上的int为 4个字节

指针为 8个字节

int num = 100;
//此时不会产生错误,但会产生警告,因为4字节的转换为8字节
void* p = (void*) num;
 
 
//此时就会产生错误,因为8字节的指针被强转为4字节的int,数据丢失了
num = (int)p;
 
//解决办法,把int 改为long
long num = 100;
 
void* p = (void*) num;
 
num = (long)p;

精彩评论(0)

0 0 举报