Retrieving IP address in C

Discussion in 'C' started by hraja, May 7, 2010.

Thread Status:
Not open for further replies.
  1. hraja

    hraja New Member

    Joined:
    Apr 6, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

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

    For example, for an IP address 1.2.3.4, 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.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,336
    Likes Received:
    377
    Trophy Points:
    83
Thread Status:
Not open for further replies.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice