title: How to send a structure containing pointers over Windows winsock send()?
body: <p>I'm creating a server/client program in C++ using Winsock.</p>
  
  <ul>
  <li><p>pack the <code>SLogin</code> and <code>Sid</code> to <code>char array</code></p></li>
  <li><p><code>Sid</code> to <code>buf[0]</code>
  and <code>SLogin</code> to <code>buf[1]</code></p></li>
  <li><p><code>Sid</code> to get from <code>buf[0]</code></p></li>
  </ul>
  
  <p>Problems in to get <code>Slogin</code> from <code>buf[1]</code>.
  This is my code:</p>
  
  <pre><code>#include &lt;Winsock2.h&gt;
  #include &lt;Windows.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string&gt;
  #pragma comment (lib, "ws2_32.lib")
  
  struct Sid
  {
      unsigned int id;
  };
  
  struct SLogin
  {
      char * login;
      char * password;
  };
  
  struct SloginRet
  {
      bool OkOrFalse;
  };
  
  SOCKET sSocket = INVALID_SOCKET;
  void Connect()
  {
  
      WSAData data;
      if(WSAStartup(MAKEWORD(2,2), &amp;data) != NO_ERROR)
      {
          printf("WSAStartup: %d\n", WSAGetLastError());
          return;
      }
  
      sSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
      if(sSocket == INVALID_SOCKET)
      {
          printf("socket: %d\n", WSAGetLastError());
          return;
      }
  
  
      sockaddr_in service;
      memset(&amp;service, 0, sizeof(service));
      service.sin_family = AF_INET;
      service.sin_addr.s_addr = inet_addr("127.0.0.1");
      service.sin_port = htons(5000);
  
      connect(sSocket, (SOCKADDR*)&amp;service, sizeof(service));
  
  
      Sid idd;
      idd.id = 1001;
      char buffer[1];
  
      memcpy(&amp;buffer[0], &amp;idd, sizeof(idd));
  
      Sid ida;
      memcpy( &amp;ida, &amp;buffer[0], sizeof(int));
  
  
      printf("%d %d\n", ida.id, sizeof(buffer));
  
      SLogin login;
  
      login.login = "Vitor";
      login.password = "123";
  
      memcpy(&amp;buffer[1], &amp;login, sizeof(char));
  
      SLogin *pLogin = (SLogin*)buffer[1]; 
      printf("%s", pLogin-&gt;login);
  //  send(sSocket, buffer,strlen(buffer), 0);
  
      closesocket(sSocket);
      WSACleanup();
  }
  
  
  void main()
  {
      Connect();
      system("pause");
  }
  
  
  Server Recv:
  
  recv(socket,buffer,1,0);
  char buffer[1]; //receive
  
  Sid id;
  memcpy(&amp;id, &amp;buffer[0],sizeof(buffer[0]));
  
  SLogin login;
  memcpy(&amp;login, &amp;buffer[1],sizeof(buffer[1]));
  </code></pre>
  
  <p>Using <code>memcpy</code> to copy <code>Sid</code> to <code>char array buf[0]</code>, (<code>char buf[1]</code>)
  and get <code>Sid</code> from buf OK.</p>
  
  <p>My Error on <code>SLogin</code>:
  I copy with <code>memcpy</code> <code>SLogin</code> login to <code>buf[1]</code> and get <code>Slogin</code> from the buf with problems.</p>