Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

uintptr_t — an integer type for holding a pointer

uintptr_t is an optional unsigned type from <stdint.h> supporting conversion from void * to an integer and back.

Version: C23; repository compiler flag, production runtime not attested

Syntax

#include <stdint.h>
/* If UINTPTR_MAX is defined: */
uintptr_t value = (uintptr_t)(void *)&object;
void *restored = (void *)value;

uintptr_t is an optional unsigned integer type from <stdint.h>, other than a bit-precise integer type. Converting a valid void * to uintptr_t and back produces a pointer that compares equal to the original (§7.22.1.4).

Check availability with #ifdef UINTPTR_MAX, not #ifdef uintptr_t: a typedef name is not a macro. Do not assume a particular width, function-pointer support, or preservation of this guarantee after arithmetic changes to the integer.

The example preserves the integer unchanged and compares pointers instead of printing a run-dependent address.

Examples

#include <stdint.h>
#include <stdio.h>

int main(void)
{
#ifdef UINTPTR_MAX
    int object = 42;
    void *original = &object;
    uintptr_t saved = (uintptr_t)original;
    void *restored = (void *)saved;
    printf("equal: %d\n", restored == original);
#else
    puts("uintptr_t unavailable");
#endif
    return 0;
}

Result

equal: 1

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources