c++ 之warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]

#include
#include

class Base
{
    public:
        int x;
        int y;
};

int main(int argc, char ** argv)
{
    Base base;

printf("base address is 0x%x.\n", &base);

    int temp = *((int*)(*(int*)&base) + 0);    
}

编译报警告.
报:warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]

原因分析:
int 在32bit和64bit系统中都占4个字节,但是,32bit指针占4个字节,64bit指针占8个字节,因此就有问题了。

修改为:
long temp = *((long*)(*(long*)&base) + 0);

你可能感兴趣的:(C/C++,c++)