title: Does the order of declaring parameters for getaddrinfo matter?
body: <p>I am writing a small server-client program using <code>getaddrinfo()</code>.</p>
  
  <p>while </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;windows.h&gt;
  #include &lt;mswsock.h&gt;
  #include &lt;ws2tcpip.h&gt;
  
  char port[] = "1933";
  char ip[]  = "192.168.178.26";
  
  int main() {
      // Prolog - init socket
      WSADATA wsaData;
      if (WSAStartup(MAKEWORD(2,1), &amp;wsaData) != 0) {
          printf("- ERROR : WSAStartup() : %d", WSAGetLastError());
          return 0;
      }
  
      int status;
      SOCKET sock;
      struct addrinfo *res = NULL;
      struct addrinfo *p = NULL;
      struct addrinfo hints;
      memset(&amp;hints, 0, sizeof(hints));
  
      hints.ai_family = AF_INET;
      hints.ai_socktype = SOCK_STREAM;
      status = getaddrinfo(ip, port, &amp;hints, &amp;res);
  
      for ( p = res; p != NULL; p = p-&gt;ai_next ) {
      printf("Before calling socket:\n\tres\t= %p\n\tp\t= %p\n", res, p);
  
          // check all returned entries
          if ( (sock = socket(p-&gt;ai_family, p-&gt;ai_socktype, p-&gt;ai_protocol)) == -1 ) {
              printf("- ERROR : socket() : ",
              WSAGetLastError());
              continue; }
  
      printf("After calling socket:\n\tres\t= %p\n\tp\t= %p\n", res, p);
  
          if ( connect(sock, p-&gt;ai_addr, p-&gt;ai_addrlen) == -1 ) {
              printf("- ERROR : connect() : ",
              WSAGetLastError());
              continue; }
  
          // socket was successfully connected
          else {
      printf("After calling connect:\n\tres\t= %p\n\tp\t= %p\n", res, p);
              printf("+ SUCCESS : socket connected\n");
              freeaddrinfo(res);
              return sock; }
      }
      freeaddrinfo(res);
      printf("- ERROR : socket could not be connected\n");
      return 0;
  }
  </code></pre>
  
  <p>works fine, changing the position of <code>struct addrinfo hints;</code> to </p>
  
  <pre><code>int status;
  SOCKET sock;
  struct addrinfo hints;
  struct addrinfo *res = NULL;
  struct addrinfo *p = NULL;
  memset(&amp;hints, 0, sizeof(hints));
  </code></pre>
  
  <p>will lead to <strong>unexpected behavior</strong> after creating the socket. In detail the <code>*res</code>is then randomly changed after the first call of <code>socket()</code>.</p>
  
  <p>I am coding with <strong>Codeblocks</strong> and a <strong>GNU GCC Compiler</strong>.</p>
  
  <p>Output of the <em>version1</em> (working):</p>
  
  <pre><code>Before calling socket:
      res = 0049FBC8
      p   = 0049FBC8
  After calling socket:
      res = 0049FBC8
      p   = 0049FBC8
  After calling connect:
      res = 0049FBC8
      p   = 0049FBC8
  + SUCCESS : socket connected
  </code></pre>
  
  <p>Output of <em>version2</em> (not working):</p>
  
  <pre><code>Before calling socket:
      res = 007DFBC8
      p   = 007DFBC8
  After calling socket:
      res = 00000002
      p   = 007DFBC8
  After calling connect:
      res = 0000007C
      p   = 007DFBC8
  + SUCCESS : socket connected
  </code></pre>