title: Is passing a pointer by reference valid for getadrinfo in C++
body: <p>I need to create a C++ socket class with a function that calls getaddrinfo() (which takes 'struct addrinfo **res' as the 4th argument).</p>
  
  <p>In order to get this to work I used a pointer by reference to pass the 'singleAddress' variable.  I read that this is bad practice but I'm not sure how to make this code work otherwise.</p>
  
  <p>The code below works perfectly. My question I guess is if using a *&amp; is good practice or if there is a better way of rewriting this call and still having a fucntion that returns a usable addrinfo structure?</p>
  
  <pre><code>#include &lt;netdb.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;string.h&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  class getAddress
  {
  public:
       int getAddressInfo(char* host, char* port, struct addrinfo *&amp;singleAddress);
  };
  
  int getAddress::getAddressInfo(char* host, char* port, struct addrinfo *&amp;singleAddress)
  {
      int ret;
      struct addrinfo addCriteria;
  
      memset(&amp;addCriteria, 0, sizeof(addCriteria));
      addCriteria.ai_family = AF_UNSPEC;
      addCriteria.ai_socktype =  SOCK_STREAM;
      addCriteria.ai_protocol = IPPROTO_TCP;
  
      ret = getaddrinfo(host, port, &amp;addCriteria, &amp;singleAddress);
      if (ret != 0)
          return -1;
  
      cout &lt;&lt; "&gt; " &lt;&lt; singleAddress-&gt;ai_family &lt;&lt; endl;
      cout &lt;&lt; "&gt; " &lt;&lt; singleAddress-&gt;ai_socktype &lt;&lt; endl;   //&lt;-- These must match
  
      return 0;
  }
  
  int main(int argc, char* argv[])
  {
      int res;
      struct addrinfo *inVal;
  
      if (argc &lt; 3)
          cout &lt;&lt; "Usage: main [domain] [port]" &lt;&lt; endl;
  
      getAddress adObj;
      res = adObj.getAddressInfo(argv[1], argv[2], inVal);
      if (res == 0)
      {
          cout &lt;&lt; "-&gt; " &lt;&lt; inVal-&gt;ai_family &lt;&lt; endl;
          cout &lt;&lt; "-&gt; " &lt;&lt; inVal-&gt;ai_socktype &lt;&lt; endl;  //&lt;-- These must match
      }
  
      return 0;
  }
  </code></pre>
  
  <p>Thanks so much!</p>