I thought the book I’m reading had typos when I read code that uses this:
uint32_t src = 0xf0;
uint32_t dest = 0x400;
int main() {
*&dest = *&src;
}
However if you take a look at the decompiled version on godbolt: link this correctly takes the value at the address stored in src, and copies it to the address in dest.
I’d love some help understanding what going on. The code looks like nonsense to me, “*&” should “cancel out” IMO.
========
Meanwhile here’s what I thought the correct code would be:
uint32_t src = 0xf0;
uint32_t dest = 0x400;
int main() {
*(uint32_t*) dest = *(uint32_t*) src;
}
Doesn’t do what’s expected, see decompiled: link. What’s wrong with this?
When the RHS is a constant it works fine, and seems to be a common pattern people use.
i think the confusion is that a variable in c is not a pointer but a referrer to a memory location. it’s been a while so maybe I’m wrong (or was never right to begin with): you attempt to cast the value storred in, e.g., src to a pointer and then get the value the pointer points to. but who knows what’s stored at location 0xf0.
the example in the book seems right: & returns the address of the variable src, and * then gets the value stored at that address.
thanks for the reply. my goal is in fact to take what is at address 0xf0 and store it at 0x400. I know it resembles another beginner error but it’s not this time :)
the example code I’ve given looks pretty contrived but I’ll be using this to write startup code for a microcontroller, and the values starting at src are initial values in flash I need to copy over to dest which is SRAM.
in your second example the right most * casts src to a pointer, saying, the value 0xf0 is a memory address, the second * then attempts to get the value stored at that address.

