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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:37:25  来源:igfitidea点击:

What is intptr_t,is it a type for integer or pointer?

clinux

提问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_tor uintptr_tand 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 a char *pointer to a uintptr_tis allowed on implementations that support the uintptr_t.

任何指向 void 的有效指针都可以在 不改变值的情况下转换为intptr_tuintptr_t并返回。C 标准保证指向 void 的指针可以转换为指向任何对象类型的指针或从指向任何对象类型的指针转​​换回来,并且结果必须与原始指针相等。因此,直接从一转换char *指针到一个uintptr_t被允许在支持的实现uintptr_t