visual studio 2012 - C problems with nested structs (looks like 1 instance is defined without explicit definitios) -


i've created struct:

typedef struct {     short s;     int i;      struct ss     {         short s;     }; } s; 

and have error (vs2012):

 error c2020: 's' : 'struct' member redefinition 

i see name "s" used, in nested named struct, shouldn't problem. know in c11 intruduced anonymouds structs , unions in case not anonymous , have vs2012 not support c11.

another interesting example is:

typedef struct {     short s;     union u     {         int uu;     };      struct s     {         short ss;     };      union     {         int i;         double d;     };  } a; 

and interesting think that: in vs2008 sizeof(a) = 24, have 1 field defined. , when use code:

a = {1, 2, 3, 4}; printf("a.s=%d a.uu=%d a.ss=%d a.i=%d \n", a.s, a.uu, a.ss, a.i); 

the output is:

a.s=1 a.uu=2 a.ss=3 a.i=4  

so question is: why that, correct behaviour?

you first example not c because part struct ss{ short s }; doesn't declare name or not anonymous member. if vs allows that, must extension. should either:

typedef struct {     short s;     int i;      struct ss     {         short s;     } m ; } s; 

or if have c11:

typedef struct {     short s;     int i;      struct  //anonymous structure     {         short s2 ;     } ; } s; 

in case name of last member must different.

just remainder anonymous structure c11:

6.7.2.1 p13

an unnamed member type specifier structure specifier no tag called anonymous structure; unnamed member type specifier union specifier no tag called anonymous union. members of anonymous structure or union considered members of containing structure or union. applies recursively if containing structure or union anonymous.

which makes anonymous structure:

struct  //note missing tag {     int name ; } ; 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -

delphi - Indy UDP Read Contents of Adata -