投长成指针 [英] cast long into pointer
本文介绍了投长成指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
本投可能会失败,是什么时候?
Can this cast fail and when?
long x=-1;
long y = (long)(void*)x;
assert(x==y);
更具体地说,如何检测,如果上面的转换是在编译时确定。
More specifically, how to detect if the above cast is OK at compile time.
推荐答案
一个更便携的方式(在C99标准的变体)是的#include< stdint.h>
再抹上指针使用intptr_t
(反面)。此整数类型保证是一个指针的大小
A more portable way (on the C99 standard variant) is to #include <stdint.h>
and then cast pointers to intptr_t
(and back). This integer type is guaranteed to be the size of a pointer.
这篇关于投长成指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文