intptr_t的用途是什么? [英] What is the use of intptr_t?

查看:733
本文介绍了intptr_t的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个整数类型,可以将其强制转换为指针/指针,而不会丢失数据,但是为什么我要这么做呢?

I know it is an integer type that can be cast to/from pointer without loss of data, but why would I ever want to do this? What advantage does having an integer type have over void* for holding the pointer and THE_REAL_TYPE* for pointer arithmetic?

编辑
标为已被问到"的问题无法回答此问题.这里的问题是,是否将intptr_t用作void*的常规替换是一个好主意,那里的答案似乎是不要使用intptr_t",所以我的问题仍然有效:什么是好的用途intptr_t是什么情况?

EDIT
The question marked as "already been asked" doesn't answer this. The question there is if using intptr_t as a general replacment for void* is a good idea, and the answers there seem to be "don't use intptr_t", so my question is still valid: What would be a good use case for intptr_t?

推荐答案

主要原因是,不能在void *上执行按位运算,但是可以在intptr_t上执行相同的操作.

The primary reason, you cannot do bitwise operation on a void *, but you can do the same on a intptr_t.

在许多情况下,需要对地址执行按位运算,则可以使用intptr_t.

On many occassion, where you need to perform bitwise operation on an address, you can use intptr_t.

但是,对于按位运算,最好的方法是使用unsigned对应的uintptr_t.

However, for bitwise operations, best approach is to use the unsigned counterpart, uintptr_t.

其他答案所述/chux> @ chux ,指针比较是另一个重要方面.

As mentioned in the other answer by @chux, pointer comparison is another important aspect.

根据C11标准§7.20.1.4

Also, FWIW, as per C11 standard, §7.20.1.4,

这些类型是可选的.

这篇关于intptr_t的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆