title: C how to serialize array of structs to char array?
body: <p>This code serialize struct to char array, and I can send it through sockets and deserialize back. </p>
  
  <p>How  to modify this code to use array of structure</p>
  
  <pre><code>server_message[n].response =  strdup("RESPONSE");
  server_message[n].command = strdup("COMMAND");
  server_message[n].data = strdup("DATA");
  </code></pre>
  
  <p>serialize to char array - </p>
  
  <pre><code>char reply[1024];
  </code></pre>
  
  <p>send through socket and deserialize back?</p>
  
  <pre><code>#include &lt;string.h&gt;
  
  
  typedef struct __attribute__((__packed__)) arch_sm
  {
      char* response;
      char* command;
      char* data;
  } request_struct_sm;
  size_t serialize(const request_struct_sm* arch_sm, char* buf)
  {
      size_t bytes = 0;
      memcpy(buf + bytes, arch_sm-&gt;response, strlen(arch_sm-&gt;response) + 1);
      bytes += strlen(arch_sm-&gt;response) + 1;
      memcpy(buf + bytes, arch_sm-&gt;command, strlen(arch_sm-&gt;command) + 1);
      bytes += strlen(arch_sm-&gt;command) + 1;
      memcpy(buf + bytes, arch_sm-&gt;data, strlen(arch_sm-&gt;data) + 1);
      bytes += strlen(arch_sm-&gt;data) + 1;
      return bytes;
  }
  
  void deserialize_server(const char* buf, request_struct_sm* arch_sm)
  {
      size_t offset = 0;
      arch_sm-&gt;response = strdup(buf + offset);
      offset += strlen(buf + offset) + 1;
      arch_sm-&gt;command = strdup(buf + offset);
      offset += strlen(buf + offset) + 1;
      arch_sm-&gt;data = strdup(buf + offset);
  }
  
  int main(){
      request_struct_sm server_message;
      request_struct_sm client_message;
      server_message.response =  strdup("RESPONSE");
      server_message.command = strdup("COMMAND");
      server_message.data = strdup("DATA");
  
    // server_message[0].response =  strdup("RESPONSE"); //Need to be able use array of structure
    //  server_message[0].command = strdup("COMMAND");
    //  server_message[0].data = strdup("DATA");
  
      char reply[1024] = {0};
      size_t bufLen = serialize(&amp;server_message, reply);
      deserialize_server(reply, &amp;client_message);
      printf("client_message.response = %s\n"
              "client_message.command = %s\n"
              "client_message.data = %s\n",
              client_message.response, client_message.command, client_message.data);
      return 0;
  
  }
  </code></pre>