title: Struct Stuffing Incorrectly
body: <p>I have the following struct:</p>
  
  <pre><code>typedef union
  {
      struct
      {
          unsigned char       ID;
          unsigned short      Vdd;
  
          unsigned char       B1State;
          unsigned short      B1FloatV;
          unsigned short      B1ChargeV;
          unsigned short      B1Current;
          unsigned short      B1TempC;
          unsigned short      B1StateTimer;
          unsigned short      B1DutyMod;
  
          unsigned char       B2State;
          unsigned short      B2FloatV;
          unsigned short      B2ChargeV;
          unsigned short      B2Current;
          unsigned short      B2TempC;
          unsigned short      B2StateTimer;
          unsigned short      B2DutyMod;
  
      } bat_values;
      unsigned char buf[64];
  } BATTERY_CHARGE_STATUS;
  </code></pre>
  
  <p>and I am stuffing it from an array as follows:</p>
  
  <pre><code>for(unsigned char ii = 0; ii &lt; 64; ii++) usb_debug_data.buf[ii]=inBuffer[ii];
  </code></pre>
  
  <p>I can see that the array has the following (arbitrary) values:</p>
  
  <pre><code>inBuffer[0] = 80;
  inBuffer[1] = 128;
  inBuffer[2] = 12;
  inBuffer[3] = 0;
  inBuffer[4] = 23;
  ...
  </code></pre>
  
  <p>now I want display these values by changing the text of a QEditLine:</p>
  
  <pre><code>str=QString::number((int)usb_debug_data.bat_values.ID);
  ui-&gt;batID-&gt;setText(str);
  str=QString::number((int)usb_debug_data.bat_values.Vdd)
  ui-&gt;Vdd-&gt;setText(str);
  str=QString::number((int)usb_debug_data.bat_values.B1State)
  ui-&gt;B1State-&gt;setText(str);
  ...
  </code></pre>
  
  <p>however, the QEditLine text values are not turning up as expected. I see the following:</p>
  
  <pre><code>usb_debug_data.bat_values.ID = 80 (correct)
  usb_debug_data.bat_values.Vdd = 12 (incorrect)
  usb_debug_data.bat_values.B1State = 23 (incorrect)
  </code></pre>
  
  <p>seems like 'usb_debug_data.bat_values.Vdd', which is a short, is not taking its value from inBuffer[1] and inBuffer[2]. Likewise, 'usb_debug_data.bat_values.B1State' should get its value from inBuffer[3] but for some reason is picking up its value from inBuffer[4].</p>
  
  <p>Any idea why this is happening?</p>