title: Converting struct to byte and back to struct
body: <p>I'm currently working with <a href="http://arduino.cc/en/Main/ArduinoBoardUno">Arduino Uno</a>s, 9DOFs, and <a href="http://en.wikipedia.org/wiki/XBee">XBee</a>s, and I was trying to create a struct that could be sent over serial, byte by byte, and then re-constructed into a struct.</p>
  
  <p>So far I have the following code:</p>
  
  <pre><code>struct AMG_ANGLES {
      float yaw;
      float pitch;
      float roll;
  };
  
  int main() {
      AMG_ANGLES struct_data;
  
      struct_data.yaw = 87.96;
      struct_data.pitch = -114.58;
      struct_data.roll = 100.50;
  
      char* data = new char[sizeof(struct_data)];
  
      for(unsigned int i = 0; i&lt;sizeof(struct_data); i++){
          // cout &lt;&lt; (char*)(&amp;struct_data+i) &lt;&lt; endl;
          data[i] = (char*)(&amp;struct_data+i); //Store the bytes of the struct to an array.
      }
  
      AMG_ANGLES* tmp = (AMG_ANGLES*)data; //Re-make the struct
      cout &lt;&lt; tmp.yaw; //Display the yaw to see if it's correct.
  }
  </code></pre>
  
  <p>Source: <a href="http://codepad.org/xMgxGY9Q">http://codepad.org/xMgxGY9Q</a></p>
  
  <p>This code doesn't seem to work, and I'm not sure what I'm doing wrong.</p>
  
  <p>How do I solve this?</p>