Quote Originally Posted by dwks View Post
That's what CornedBee meant. You can't do it portably; you have to assume things like structure padding. You shouldn't be writing code that relies on this sort of thing, unless you're doing something pretty unusual.
If I'm not mistaken, the compiler is responsible for the padding, not something else. You can force a compiler or set it in the options to set padding. Or you can just write a mechanism to parse memory and figure it out. Either way, it does become portable.
And actually, I have once written something "unusual" that does something like this...

I was writing a small library for DirectInput w/ action feedback or whatever it's called. The program registered what keys it wanted to intercept and passed along a struct of bool values and the number of keys it has registered, that were set to true if key was pressed down and false if not.
Since the library had no idea of the contents of the struct, I used a raw memory approach. If key 4 was pressed, set beginning_of_struct + 4 to true, and so on.
Why did I do this? Why not an array or something? The approach to this is that the calling program can define the structure however they want as long as there's one member for each key. Then you can name the members whatever instead of a generic "bool key4;"