How to pass an integer value to a pointer address without triggering warnings

I have the following variable

uint32_t Value = 0x80

0x80 is an address in memory, for example.

// Write 2 at address 0x80
*(uint32_t*)((uint32_t)0x80) = 2;

How can I assign a value to a pointer, so it points to 0x80?

uint32_t *Pointer = ?? Value;

It:

(uint32_t*)(uint32_t)Value;

returns:

warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
+4
source share
2 answers

To handle the conversion of an integer pointer to an object, use the optional integer types uintptr_tor intptr_t. Function pointers are a separate issue.

, void , void, C11dr 7.20.1.4 1

uintptr_t

void * .

#include <stdint.h>
uintptr_t Value = 0x80;
uint32_t *Pointer = (void *) Value;

0x80 uint32_t *, undefined (UB). , , OP .

+2

:

uint32_t Value = 0x80;

*((uint32_t *)(uintptr_t)Value) = 2;

uintptr_t, , , . , , , , , .

; , "cpp plus 1" , size_t. , , ABI , size_t, , , , .

Value :

uint32_t *const Value = (uint32_t *)(uintptr_t)0x80;

, , ,

*Value = 2;

, , , . , , sizeof(uintptr_t) < sizeof(uint32_t), uint32_t ; , 0x80 uintptr_t , , .

+1

Source: https://habr.com/ru/post/1681890/


All Articles