【问题标题】:How to initialize struct in6_addr?如何初始化 struct in6_addr?
【发布时间】:2010-12-15 01:09:21
【问题描述】:

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

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 的括号。为什么?

谢谢。

【问题讨论】:

    标签: network-programming ipv6


    【解决方案1】:

    执行此操作的可移植方式如下:

    struct in6_addr s6 = { };
    if (!IN6_IS_ADDR_UNSPECIFIED(&s6))
      inet_pton(AF_INET6, "2001:db8::1", &s6);
    

    【讨论】:

      【解决方案2】:

      因为需要指出它所寻址的地址形式(使用 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;
        };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-04-08
        • 2015-11-08
        • 1970-01-01
        • 2017-07-21
        • 2021-03-19
        • 2011-07-29
        • 2022-01-13
        相关资源
        最近更新 更多