我正在编译一段代码,它在以下函数中给出了错误:
inline char *align(char *var, DataType_e type)
{
return (DataTypeSize[type] == 0) ? var :
(char*) (((unsigned int)(var) + DataTypeSize[type]-1) /
DataTypeSize[type] * DataTypeSize[type]);
}
以下错误符合“(unsigned int)(var)”:
error: cast from 'char*' to 'unsigned int' loses precision
如果我将“unsigned int”更改为“unsigned long”,编译工作正常,但在运行我的程序时我没有得到预期的结果。关于如何解决此问题的任何想法?
最佳答案
在 C 中,如果需要将指针转换为整数类型(但首先应避免使用),则应使用 [u]intptr_t
。
如果存在,这些类型保证不会丢失信息。
关于c++ - 错误 : cast from 'char*' to 'unsigned int' loses precision,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11207956/