How can I store a value at a specific location in the memory?

You can do it like this:

*(int *)0x16 = 10;  // store int value 10 at address 0x16

Note that this assumes that address 0x16 is writeable – in most cases this will generate an exception.

Typically you will only ever do this kind of thing for embedded code etc where there is no OS and you need to write to specific memory locations such as registers, I/O ports or special types of memory (e.g. NVRAM).

You might define these special addresses something like this:

volatile uint8_t * const REG_1 = (uint8_t *) 0x1000;
volatile uint8_t * const REG_2 = (uint8_t *) 0x1001;
volatile uint8_t * const REG_3 = (uint8_t *) 0x1002;
volatile uint8_t * const REG_4 = (uint8_t *) 0x1003;

Then in your code you can read write registers like this:

uint8_t reg1_val = *REG_1; // read value from register 1
*REG_2 = 0xff;             // write 0xff to register 2

Leave a Comment