我知道一种方法做到这一点,

const struct in6_addr naddr6 = { { 
                0x3f, 0xfe, 0x05, 0x01,
                0x00, 0x08, 0x00, 0x00, 
                0x02, 0x60, 0x97, 0xff,
                0xfe, 0x40, 0xef, 0xab
}};

但不能与此,

const struct in6_addr naddr6 = 
                { { { 0x3ffe0501, 0x00080000, 0x026097ff, 0xfe40efab } } }; 

和似乎我既可以1/2/3巴黎brackets.Why的?

感谢。

有帮助吗?

解决方案

由于一个需要以表明它是寻址(示出了使用C99)的地址的哪格式:

const struct in6_addr naddr6 = 
    { { .u6_addr32 = { 0x3ffe0501, 0x00080000, 0x026097ff, 0xfe40efab } } };

在第一托架对是用于in6_addr结构,第二个为联合:

struct in6_addr
  {
    union
      {
 uint8_t u6_addr8[16];
 uint16_t u6_addr16[8];
 uint32_t u6_addr32[4];
      } in6_u;
  };

其他提示

的便携式方式做到这一点是像这样:

struct in6_addr s6 = { };
if (!IN6_IS_ADDR_UNSPECIFIED(&s6))
  inet_pton(AF_INET6, "2001:db8::1", &s6);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top