I'm writing an emulator in C. Its memory is byte-addressible so I'm using a char array, but I need to read/write unaligned 32-bit integers.
Currently I'm using *((unsigned int*) &memory[address]), but it seems pretty horrible. What's the best way to do it?
intis not exactly 32 bits, so if you really mean "four bytes" rather than "sizeof(int)", and you care about portability and maintainability, you need to do something different.memcpy()or other byte-wise copy that can deal with unaligned addresses. I don't know if this is important to you, but if it is it's something you need to think about.