将指针转换成整数整数、转换成、指针

2023-09-07 17:51:26 作者:獵杀浪人

我想,以适应现有的code到64位的机器。的主要问题是,在一个功能,previous codeR使用一个void *论点,即转换成合适的类型的函数本身。简单例子:

I am trying to adapt an existing code to a 64 bit machine. The main problem is that in one function, the previous coder uses a void* argument that is converted into suitable type in the function itself. A short example:

void function(MESSAGE_ID id, void* param)
{
    if(id == FOO) {
        int real_param = (int)param;
        // ...
    }
}

当然,在64位机器上,我得到的错误:

Of course, on a 64 bit machine, I get the error:

error: cast from 'void*' to 'int' loses precision

我想纠正这一点,以便它仍然工作在32位机器上,并且尽可能干净。你知道吗?

I would like to correct this so that it still works on a 32 bit machine and as cleanly as possible. Any idea ?

推荐答案

为size_t'和'ptrdiff_t型的要求,以符合您的体系结构(不管它是什么)。所以,我觉得,而不是使用'廉政',你应该能够使用'为size_t',它在64位系统应该是一个64位的类型。

'size_t' and 'ptrdiff_t' are required to match your architecture (whatever it is). Therefore, I think rather than using 'int', you should be able to use 'size_t', which on a 64 bit system should be a 64 bit type.

此讨论 unsigned int类型VS为size_t 进入更详细一点。

This discussion unsigned int vs size_t goes into a bit more detail.