Retrieving IP address in C

hraja's Avatar, Join Date: Apr 2010
Light Poster

I am having trouble retrieving the IP address and saving it into an array.

For example, for an IP address, I wish to have my array as a[0]=1; a[1]=2; etc. The following is the code I have been working on.

#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>

int main ()
int sock,i;
char dataarray[32];
char name[255];
struct in_addr min_addr;
min_addr = *(struct in_addr *)*gethostbyname(name)->h_addr_list;

dataarray[0] = min_addr.S_un.S_un_b.s_b1;
dataarray[1] = min_addr.S_un.S_un_b.s_b2;
dataarray[2] = min_addr.S_un.S_un_b.s_b3;
dataarray[3] = min_addr.S_un.S_un_b.s_b4;

I get the following error on compiling:

error: ‘struct in_addr’ has no member named ‘S_un’

It would be great if I could get some help on this. I hope I have included the correct header files necessary to support these methods.

Many thanks.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Duplicate of

Thread Closed. Please avoid posting the same things more than once.