Why is sin_addr inside the structure in_addr?

c, sockets, unix

Solution

`struct in_addr` is sometimes very different than that, depending on what system you're on. On Windows for example:

typedef struct in_addr {
  union {
    struct {
      u_char s_b1,s_b2,s_b3,s_b4;
    } S_un_b;
    struct {
      u_short s_w1,s_w2;
    } S_un_w;
    u_long S_addr;
  } S_un;
} IN_ADDR, *PIN_ADDR, FAR *LPIN_ADDR;

The only requirement is that it contain a member `s_addr`.

Problem

My doubt is related to the following structure of sockets in UNIX : ``` struct sockaddr_in { short sin_family; // e.g. AF_INET, AF_INET6 unsigned short sin_port; // e.g. htons(3490) struct in_addr sin_addr; // see struct in_addr, below char sin_zero[8]; // zero this if you want to }; ``` Here the member `sin_addr` is of type `struct in_addr`. But I don't get why someone would like to do that as all `struct inaddr` has is : ``` struct in_addr { unsigned long s_addr; // load with inet_pton() }; ``` All `in_addr` has is just one member `s_addr`. Why cannot we have something like this : ``` struct sockaddr_in { short sin_family; // e.g. AF_INET, AF_INET6 unsigned short sin_port; // e.g. htons(3490) unsigned long s_addr ; char sin_zero[8]; // zero this if you want to }; ```

Original source