Inconsistent behaviour using const and anonymous structs

Started by neo313, June 09, 2014, 06:39:27 AM

Previous topic - Next topic

neo313

I have stumbled upon some inconsistency when using anonymous structs in c11 mode. I believe const qualifier is ignored where it shouldn't be.



struct A
{
const int ci ;
} ;

struct B
{
struct A ;
} ;

struct C
{
struct
{
const int ci ;
} mid ;
} ;

int main(void)
{
struct A a1 = { 11 } ;
struct A a2 ;

a2 = a1 ; //warns correctly since it contains a const member ci
a1.ci = 22 ; //warns



struct B b1 = { 33 } ;
struct B b2 ;

b2 = b1 ; //doesn't warn about assigning to a const member
//6.7.2.1, paragraph 13 : The members of an anonymous structure or union
//are considered to be members of the containing structure or union.
//that would make struct B identical to struct A, yet const is ignored

b1.ci = 44 ; //warns since member is const



struct C c1 = { 55 } ;
struct C c2 ;

c2 = c1 ; //doesn't warn, this time an anonymous struct is used, same situation as with struct B
c2.mid = c1.mid ; //this time warns correctly even though(!) we are assigning anonymous structs

    return 0;
}

Pelle

Not anynomous I think: it has no name, but it has a tag. Looks fishy anyway, I will have a look...
/Pelle

neo313

Thank you.

Yes, struct C does not have an anonymous struct member, my mistake.