Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Types and Literals

C intptr_t and uintptr_t

Reference for intptr_t and uintptr_t in C: integers that can hold a pointer, round-trip guarantees, alignment checks, pointer tagging and portability caveats.

intptr_t and uintptr_t, from <stdint.h>, are integer types large enough to hold a void *. The standard guarantees that you can convert a valid void * to one of them and back to void *, and the result compares equal to the original pointer. They are optional in theory, but available on all common platforms.

Use them when you need to treat an address as a number: to test alignment, to hash a pointer, to store a pointer in a generic integer slot, or to compute the distance between two unrelated objects for debugging. They are a precise tool and the wrong one for ordinary pointer arithmetic.

C
#include <stdint.h>

uintptr_t address = (uintptr_t)ptr;
void *same = (void *)address;

Try it

C Round trip and alignment test

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Rounding addresses up to an alignment

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
#include <stdint.h>

intptr_t  signed_number;
uintptr_t unsigned_number;
Item Description
intptr_t Signed integer type that can hold a void *.
uintptr_t Unsigned counterpart. The usual choice for bit tricks.
INTPTR_MIN, INTPTR_MAX, UINTPTR_MAX Limits from <stdint.h>.
PRIxPTR, PRIuPTR, SCNxPTR printf/scanf macros from <inttypes.h>.

The guarantee is one-way: void * to integer to void * returns the same pointer. The standard does not say what the numeric value means, that it is the byte address, or that nearby objects have nearby numbers.

Errors / undefined behavior: converting an integer that never came from a pointer to a pointer and dereferencing it is undefined unless the implementation says otherwise. Arithmetic you perform on the integer can produce a pointer that points outside any object; using it is undefined.

Usage notes

  • Casting is implementation-defined, though GCC and Clang treat the value as the real address on all mainstream targets.
  • Prefer uintptr_t for bit operations; shifts and masks on signed values are error-prone.
  • Printing addresses: use %p with a (void *) cast for pointers. For the numeric value use "%" PRIxPTR. This site avoids printing addresses because they change from run to run.
  • Pointer tagging (storing flags in the low bits of aligned pointers) works with uintptr_t, but compilers with pointer provenance rules or hardware features such as ARM pointer authentication can break it. Mask the tag off before using the pointer.
  • Do not use it to compare pointers into different objects unless you only need an arbitrary but consistent order for a map. Even then, results are only reliable on flat memory models.
  • Do not store pointers in int or long: on 64-bit Windows long is 32 bits and truncates addresses.
  • Alignment checks like address % 8 == 0 are the typical, portable use.
  • Added in C99. In C++ they live in <cstdint>.