Syntax request used in header file for socket programming

This is part of the code that I copied from one of the header files for socket programming.

/* Structure describing an Internet socket address.  */
struct sockaddr_in
  {
    __SOCKADDR_COMMON (sin_);
    in_port_t sin_port;                 /* Port number.  */
    struct in_addr sin_addr;            /* Internet address.  */

    /* Pad to size of `struct sockaddr'.  */
    unsigned char sin_zero[sizeof (struct sockaddr) -
                           __SOCKADDR_COMMON_SIZE -
                           sizeof (in_port_t) -
                           sizeof (struct in_addr)];
  };

I can understand the declaration of sin_port and sin_addr. but what is __SOCKADDR_COMMON in_). I can not understand this syntax? Explain, please. Thanks in advance.

+3
source share
3 answers

There is a macro definition:

#define __SOCKADDR_COMMON(sa_prefix) \
  sa_family_t sa_prefix##family

therefore __SOCKADDR_COMMON (sin_);actually expanding tosa_family_t sin_family;

As this happens, the macro takes the sa_prefix parameter and uses the operator ##to combine (combine). As a result, you have a new variable sin_familythat is declared with a type sa_family_tin the structure.

C

+4

, - .

#define __SOCKADDR_COMMON(sa_prefix) \
  sa_family_t sa_prefix##family

,

struct sockaddr_in
  {
    sa_family_t sin_family;
    in_port_t sin_port;      
 ... 

. , struct sockaddr_in.

0

__SOCKADDR_COMMON()will be a macro #definethat extends the set of common fields that will be used in all structures

0
source

Source: https://habr.com/ru/post/1767327/


All Articles