It comes down to the following:
((const MsgHeader*)rxBuffer)->msgType
rxBuffer has one type, but we want to consider it as if from another type. I suggest the following "alias-cast":
const MsgHeader * msg_header_p = (const MsgHeader *) rxBuffer; memmove(msg_header_p, rxBuffer, sizeof(MsgHeader)); auto msg_type = msg_header_p -> msgType;
memmove (as its less flexible cousin memcpy ) effectively says that the bit pattern that was available in the source ( rxBuffer ), after calling memmove will be available at the destination ( msg_header_p ). Even if the types are different.
You can argue that memmove does nothing because the source and destination are identical. But that's for sure. Logically, it serves to create an msg_header_p alias for rxBuffer , although in practice a good compiler optimizes it.
(This answer is potentially somewhat contradictory. Perhaps I memmove too hard. I assume my logic is this: first, memcpy to a new place is clearly acceptable to answer this question, and secondly, memmove (but maybe slower) , memcpy version; thirdly, if memcpy allows you to look at the same bit pattern with a different type, then why not memmove allow the same idea to “change” the type of a specific bit pattern? If we memcpy in the time domain, then memcpy will return to the starting position, will it be OK too?)
If you want to build a complete answer from this, you will need to run the alias in memmove(rxBuffer, msg_header_p, sizeof(MsgHeader)); but I think I should wait for feedback on my "alias cast" first!
Aaron mcdaid
source share