NO

Author Topic: Inconsistent behaviour using const and anonymous structs  (Read 2037 times)

neo313

  • Guest
Inconsistent behaviour using const and anonymous structs
« on: June 09, 2014, 06:39:27 AM »
I have stumbled upon some inconsistency when using anonymous structs in c11 mode. I believe const qualifier is ignored where it shouldn't be.


Code: [Select]
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;
}
« Last Edit: June 09, 2014, 07:00:27 AM by neo313 »

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: Inconsistent behaviour using const and anonymous structs
« Reply #1 on: June 09, 2014, 06:28:09 PM »
Not anynomous I think: it has no name, but it has a tag. Looks fishy anyway, I will have a look...
/Pelle

neo313

  • Guest
Re: Inconsistent behaviour using const and anonymous structs
« Reply #2 on: June 09, 2014, 06:34:55 PM »
Thank you.

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