That code fragment is dependent on the hostname. So, you must query the hostname in order to get the IP address. If you're determined to only get the IP address, then use the iphlpapi.lib helper lib.
Code:
#pragma comment( lib, "iphlpapi.lib" )
#include <windows.h>
#include <stdio.h>
#include <iphlpapi.h>
int main(void)
{
ULONG dwSize = 0;
DWORD dwIP;
// We just get the table size here
if(GetIpAddrTable(NULL, &dwSize, FALSE) == ERROR_INSUFFICIENT_BUFFER)
{
// Allocate a pointer based on table size
MIB_IPADDRTABLE *ipAddress = (MIB_IPADDRTABLE *) new BYTE[dwSize];
// Now here's where we retrieve the actual info
if(GetIpAddrTable(ipAddress, &dwSize, TRUE) == NO_ERROR)
{
// We may have multiple interfaces. Let's list them all
for(DWORD dwIndex = 0; dwIndex < ipAddress->dwNumEntries; dwIndex++)
{
//The IP address
dwIP = ipAddress->table[dwIndex].dwAddr;
// Assuming Windows, we'll use a macro conversion
// You can also use inet_ntoa here to address endianess
int octet1 = LOBYTE(LOWORD(dwIP));
int octet2 = HIBYTE(LOWORD(dwIP));
int octet3 = LOBYTE(HIWORD(dwIP));
int octet4 = HIBYTE(HIWORD(dwIP));
printf("%d.%d.%d.%d\n",octet1, octet2, octet3, octet4);
}
}
}
return 0;
}