I will spell it out for you: given
uint32_t Value = 0x80;
you want
*((uint32_t *)(uintptr_t)Value) = 2;
The type uintptr_t
, if it exists, is guaranteed to be castable to and from arbitrary pointer types without loss of information. It is not guaranteed to exist, but on platforms where it doesn't exist, what you're trying to do cannot safely be done without a great deal more information.
No other type is officially guaranteed to have this property; however, "cpp plus 1" is correct that size_t
usually also does. I would go so far as to say that any ABI for a flat-memory architecture, that doesn't make size_t
have this property, was specified incorrectly, but they do exist and sometimes you have to work with them.
It would be better to declare Value
with a pointer type in the first place:
uint32_t *const Value = (uint32_t *)(uintptr_t)0x80;
because then you only have to write the casts when you initialize it, not when you use it,
*Value = 2;
and you probably have a bunch of places where you use it. This also avoids a potential problem if it happens that sizeof(uintptr_t) < sizeof(uint32_t)
, as uint32_t
is never involved in the address arithmetic; the compiler may complain if the cast from 0x80
to uintptr_t
actually truncates the constant, but that shouldn't ever happen with the real memory addresses you are accessing this way.