Linux 什么是intptr_t,它是整数类型还是指针类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6410294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is intptr_t,is it a type for integer or pointer?
提问by compile-fan
It's defined in /usr/include/stdint.h
:
它定义在/usr/include/stdint.h
:
typedef long int intptr_t;
is it supposed to be a type for integer or pointer?
它应该是整数类型还是指针类型?
回答by Richard Pennington
It is a signed integer type that is big enough to hold a pointer.
它是一个大到足以容纳一个指针的有符号整数类型。
回答by the kamilz
It is a signed integer type that guaranteed to can hold a void*
type.
它是一个有符号整数类型,保证可以容纳一个void*
类型。
And why there is also [u]intptr_t
? Because:
为什么还有[u]intptr_t
?因为:
Any valid pointer to void can be converted to
intptr_t
oruintptr_t
and back with no change in value. The C Standard guarantees that a pointer to void may be converted to or from a pointer to any object type and back again and that the result must compare equal to the original pointer. Consequently, converting directly from achar *
pointer to auintptr_t
is allowed on implementations that support theuintptr_t
.
任何指向 void 的有效指针都可以在 不改变值的情况下转换为
intptr_t
或uintptr_t
并返回。C 标准保证指向 void 的指针可以转换为指向任何对象类型的指针或从指向任何对象类型的指针转换回来,并且结果必须与原始指针相等。因此,直接从一转换char *
指针到一个uintptr_t
被允许在支持的实现uintptr_t
。