title: Copy two structs in C that contain char pointers
body: <p>What is the standard way to copy two structs that contain char arrays?</p>
  
  <p>Here is some code:</p>
  
  <pre><code>#include stdio.h&gt;
  #include string.h&gt;
  #include stdlib.h&gt;
  
  typedef struct {
      char* name;
      char* surname;
  } person;
  
  int main(void){
      person p1;
      person p2;
  
      p1.name     = (char*)malloc(5);
      p1.surname  = (char*)malloc(5);
  
      strcpy(p1.name, "AAAA");
      strcpy(p1.surname, "BBBB");
  
      memcpy(&amp;p2, &amp;p1, sizeof(person));
      free(p1.name);
      printf("%s\n", p2.name);
      return 0;
  }
  </code></pre>
  
  <p>The line <code>printf("%s\n", p2.name);</code> does not print something, because I freed the buffer.</p>
  
  <p>The problem with my structs is that they are bigger than struct <code>person</code>. They contain hundreds of char pointers, and I have to copy every member one by one.</p>
  
  <p>Is there another way to copy two structs that contain char arrays without using <code>malloc</code> and <code>strcpy</code> for every member?</p>