title: Copying a struct into a byte array
body: <p>I have a 1-byte pragma packed struct in C which I want to copy into a byte array for serialization purpose to be sent over a serial port.</p>
  
  <pre><code>#pragma pack(push, 1)
  
  typedef struct {
      uint8_t ck_a;
      uint8_t ck_b;
  } UBXChecksum_t ;
  
  #pragma pack(pop)
  </code></pre>
  
  <p>What is the best way of serializing it into a byte array, should I just use <code>memcpy()</code>?</p>
  
  <pre><code>void writeStructToArray(const void* inStruct,
                          const uint16_t inLenStruct,
                          uint8_t* const outArray)
  {
    memcpy(outArray, inStruct, inLenStruct);
  }
  </code></pre>
  
  <p>or better use byte-by-byte copying doing pointer typecasting?</p>
  
  <pre><code>void writeStructToArray(const void* inStruct,
                          const uint16_t inLenStruct,
                          uint8_t* const outArray)
  {
    for(uint16_t i = 0; i &lt; inLenStruct; i++)
    {
      outArray[i] = ((uint8_t*)inStruct)[i];
    }
  }
  </code></pre>