title: Print all IPs based on IP and mask C++
body: <p>I would like to print all the possible IPs for a given mask. I have this code to get it but it seems I'm missing something since I can't get the list of IPs. I've based my code in <a href="https://stackoverflow.com/questions/19728127/function-that-return-a-list-of-ip-addresses-for-a-given-ip-adress-and-subnetmask">this other post</a>.</p>
  
  <pre><code>unsigned int ipaddress, subnetmask;     
  
  inet_pton(AF_INET, b-&gt;IpAddressList.IpAddress.String, &amp;ipaddress);
  inet_pton(AF_INET, b-&gt;IpAddressList.IpMask.String, &amp;subnetmask);
  
  for (unsigned int i = 1; i&lt;(~subnetmask); i++) {
      auto ip = ipaddress &amp; (subnetmask + i);
  }
  </code></pre>
  
  <p>Example: ipaddress= 172.22.0.65 netmask= 255.255.252.0</p>
  
  <p>I expect:</p>
  
  <pre>
  172.22.0.1
  172.22.0.2
  172.22.0.3
  172.22.0.4
  ...
  </pre>
  
  <p><strong>Update</strong>: I tried this code, but it does not work, either:</p>
  
  <pre><code>char* ip = "172.22.0.65";
  char* netmask = "255.255.252.0";
  
  struct in_addr ipaddress, subnetmask;
  
  inet_pton(AF_INET, ip, &amp;ipaddress);
  inet_pton(AF_INET, netmask, &amp;subnetmask);
  
  unsigned long first_ip = ntohl(ipaddress.s_addr &amp; subnetmask.s_addr);
  unsigned long last_ip = ntohl(ipaddress.s_addr | ~(subnetmask.s_addr));
  
  for (unsigned long ip = first_ip; ip &lt;= last_ip; ++ip) {
      unsigned long theip = htonl(ip);
      struct in_addr x = { theip };
      printf("%s\n", inet_ntoa(x));
  }
  </code></pre>