NO

Author Topic: Unable to initialize struct with a struct  (Read 2223 times)

neo313

  • Guest
Unable to initialize struct with a struct
« on: June 06, 2014, 11:44:22 PM »
This bug is present with any optimization level and both c99 or c11 standard mode, using: 8.00.11 RC#4, 32bit , Windows 7

error text: Invalid initialization type; expected 'int' but found 'struct A'.

Code: [Select]
#include <stdio.h>

struct A
{
    int i;
} ;

struct B
{
   struct A st ;
} ;

int main( void )
{
    struct A a = { 12345 } ;
    struct B b = { a };    //Invalid initialization type; expected 'int' but found 'struct A'.

    printf("%d" , b.st.i ) ;

    return 0;
}

Same error happens if struct is initialized using designated initializers struct B b = { .st =  { a } }; or struct B b = { .st =  a };

This is surely allowed according to C Standard. Reference: 6.7.9, paragraph 13

Ideone example compiles in strict C99 mode:
https://ideone.com/FWSqak

Relevant bug reports:
https://stackoverflow.com/questions/23707402/initializing-struct-within-another-struct-using-designated-initializer-causes-co
http://stackoverflow.com/questions/24090739/possible-compiler-bug-in-msvc12-vs2013-with-designated-initializer#24090739
« Last Edit: June 07, 2014, 10:19:34 PM by neo313 »

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: Unable to initialize struct with a struct
« Reply #1 on: June 09, 2014, 06:30:23 PM »
A known, long standing, bug. This case is hard to fix in this compiler. Will take time...
/Pelle

neo313

  • Guest
Re: Unable to initialize struct with a struct
« Reply #2 on: June 09, 2014, 06:37:16 PM »
Thanks, I didn't encounter this myself; I still reported it just to be sure.
« Last Edit: June 09, 2014, 06:50:18 PM by neo313 »