uintptr_t
and Other Helpful TypesOther useful types provided by <inttypes.h> include signed
and unsigned integer types large enough to hold a pointer. These are given as intptr_t
and uintptr_t
. In addition, intmax_t
and uintmax_t
are defined to be the longest (in bits) signed and unsigned integer
types available.
Using the uintptr_t
type as the integral type for pointers is a
better option than using a fundamental type such as unsigned long
. Even
though an unsigned long
is the same size as a pointer in both the ILP32
and LP64 data models, the use of the uintptr_t
requires only the definition
of uintptr_t
to change when a different data model is used. This makes
it portable to many other systems. It is also a clearer way to express your intentions
in C.
The intptr_t
and uintptr_t
types are extremely useful
for casting pointers when you want to do address arithmetic. They should be used instead
of long
or unsigned long
for this purpose.
Use of uintptr_t
for casting is usually safer than intptr_t
, especially for comparisons.