0

I was studying about socket programming using C. In the header file netinet/in.h there is this code

struct in_addr
{
     in_addr_t s_addr;
};

and this structure is used in the following structure.

struct sockaddr_in
{
      short   sin_family; /* must be AF_INET */
      u_short sin_port;
      struct  in_addr sin_addr;
      char    sin_zero[8]; /* Not used, must be zero */
};

What could be the reason of creating in_addr structure, while they could have just used it as a normal variable?

rjmrohit
  • 11
  • 4
  • 1
    abstraction there are quite a few variants of struct sockaddr_* and corresponding *_addr – asio_guy Oct 12 '16 at 12:07
  • 2
    Take a look at [this SO post](http://stackoverflow.com/questions/13979150/why-is-sin-addr-inside-the-structure-in-addr) – LPs Oct 12 '16 at 12:08

1 Answers1

0

Thats an abstraction layer. The rest of the code is written platform agnostic, and what exactly is in struct in_addr is presumably different on different platforms. This abstraction is done so the rest of the code can be platform agnostic.

Magisch
  • 7,312
  • 9
  • 36
  • 52