2012-03-03 66 views
0
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; 

in_addr srcip 

简单in_addr结构,我注意到,我只能参考S_addr通过srcip.S_addrsrcip.S_un.S_addr不起作用。我对此做了一个测试。引用变量的联合

struct test_struct { 
     union { 
      int m; 
      int n; 
     }test; 
    }; 
    test_struct x; 
    x.test.m = 1; 

编译successfuly,但是当我改变x.test.m = 1x.m = 1,编译失败。为什么这两个相似的条件不相容?

srcip.S_addr成功,srcip.S_un.S_addr失败

x.test.m成功,但失败了x.m,完全不一样!

使用VS2008和Win7

回答

1

要声明在你的问题中in_addr结构,既可以使用的typedef:

IN_ADDR srcip; 

struct in_addr srcip; 

那么你应该能够使用srcip.S_un.S_addr

通过执行in_addr srcip,您可能会声明其他名为in_addr的其他名称在命名空间中可用。

+1

+1:发现了! – 2012-03-03 03:49:42