在C中明确演员

Arj*_*gri -1 c c++ casting alignment type-conversion

我想在C和C++中将任何类型的指针转​​换为int.C和C++的代码必须相同.以下程序适用于C但不适用于C++.

int main(void)
{
    long long a;
    int b = (int)&a;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能让它适用于C++?

Jed*_*Jed 6

看起来你想要offsetof宏,它是在中定义的<stddef.h>.

编辑:你已经改变了你的例子,现在你应该看看intptr_t或者是uintptr_t哪个stdint.h.在任何情况下,都不要把地址写成平原int.