Content originally posted in LPCWare by TheFallGuy on Fri May 06 07:21:58 MST 2011
There is nothing in the C language spec that says that variables defined consecutively have to be located consecutively, and thus compilers (and linkers) are free to do what they want with the locations. Very often, compilers/linkers will move things about for either size, performance or target architecture reasons.
Using a struct will cause the compiler to place your variables together. However, depending on the data types, they may not necessarily be consecutive, due to (possible) target architecture restrictions. For example, some architectures require data to be located on word or halfword boundaries. To get even better control of how to layout struct's, you will need to pack them. Packing is a compiler-specific extension and so you need to read your compiler documentation. For GNU compilers, you decorate your struct definition with __attribute__ ((packed)). Most people define this as a macro so they can easily change it between compilers.
As I think you are defining two 16-bit variables, using GCC and ARM, you will be OK defining them in a struct and will not need to pack them. However, if you might want to use a different compiler or port to another architecture, you might want to consider packing.
Hope that helps,