title: How to copy the contents of an unpacked struct to a __packed__ struct?
body: <p>I read about <code>__packed__</code> from <a href="http://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html#Type-Attributes" rel="nofollow">here</a> and, I understood that when <code>__packed__</code> is used in a <code>struct</code> or <code>union</code>, it means that the member variables are placed in such a way to minimize the memory required to store the <code>struct</code> or <code>union</code>.</p>
  
  <p>Now, consider the structures in the following code. They contain same elements (same <code>type</code>, same variable names and placed in the same order). The difference is, one is <code>__packed__</code> and the other is not. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main(void)
  {
  
      typedef struct unpacked_struct {
          char c;
          int i;
          float f;
          double d;
      }ups;
  
      typedef struct __attribute__ ((__packed__)) packed_struct {
          char c;
          int i;
          float f;
          double d;
      }ps;
  
      printf("sizeof(my_unpacked_struct)  : %d \n", sizeof(ups));
      printf("sizeof(my_packed_struct)    : %d \n", sizeof(ps));
  
      ups ups1 = init_ups();
      ps ps1;
  
      return 0;
  }
  </code></pre>
  
  <p>Is there a way where we can copy unpacked structure <code>ups1</code> into packed structure <code>ps1</code> without doing a <code>member-variable-wise-copy</code>? Is there something like <code>memcpy()</code> that is applicable here?</p>