title: How to override C compiler aligning word-sized variable in struct to word boundary
body: <p>I have a structure specified as following</p>
  
  <ul>
  <li>Member 1, 16 bits</li>
  <li>Member 2, 32 bits</li>
  <li>Member 3, 32 bits</li>
  </ul>
  
  <p>which I shall be reading from a file. I want to read straight from the file into the struct.</p>
  
  <p>The problem is that the C compiler will align the variables m1, m2 and m3 to word boundaries which are at 32 bits since I am working on an ARM Cortex M3 for the following struct declaration:</p>
  
  <pre><code>typedef struct
  {
      uint16_t m1;
      uint32_t m2;
      uint32_t m3;
  }something;
  </code></pre>
  
  <p>Reading directly from file will put wrong values in m2 and m3, and reads 2 extra bytes too.</p>
  
  <p>I have hacked around and am currently using the following which works just fine:</p>
  
  <pre><code>typedef struct
  {
      uint16_t m1;
      struct
      {
          uint16_t lo;
          uint16_t hi;
      }m2;
      struct
      {
          uint16_t lo;
          uint16_t hi;
      }m3;
  }something;
  </code></pre>
  
  <p>However, this looks like a really dirty hack. I cannot help wishing for a cleaner way to force the compiler to put halves of m2 and m3 in different words, however sub-optimal it may be.</p>
  
  <p>I am using arm-none-eabi-gcc. I know about bit packing, but am unable to work around this optimisation.</p>
  
  <p>Edit: Turns out I didn't know <strong>enough</strong> about bit-packing :D</p>