This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Struct inside union init error (arm_15.12.3.LTS)

Hello!

I get compilation error when try to init struct inside union using filed names. Test code attached. No preproccessor directives used. Error always triggered on second field initialization.

C:\ti\ccsv6\tools\compiler\arm_15.12.3.LTS\bin\armcl.exe init_error.c
"init_error.c", line 43: error: union "<unnamed>" has no field "s1_f2"
1 error detected in the compilation of "init_error.c".

>> Compilation failure

typedef struct {
    int f1;
    int f2;
    union {
        struct {
            int s1_f1;
            int s1_f2;
            int s1_f3;
        } s1;
        struct {
            int s2_f1;
            int s2_f2;
            int s2_f3;
        } s2;
    };
} test_struct_t;


//Works
test_struct_t t1 = {
    .f1 = 0,
    .f2 = 0,
    .s1.s1_f1 = 0,
    .s1.s1_f2 = 0,
    .s1.s1_f3 = 0, };

//Works
test_struct_t t2 = {
    0,
    0,
    {0,
    0,
    0,}
};


//Compilation error:  union "<unnamed>" has no field "s1_f2"
test_struct_t t3 = {
    .f1 = 0,
    .f2 = 0,
    .s1 = {
        .s1_f1 = 0,
        .s1_f2 = 0,
        .s1_f3 = 0, }, };

init_error.c
Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
typedef struct {
int f1;
int f2;
union {
struct {
int s1_f1;
int s1_f2;
int s1_f3;
} s1;
struct {
int s2_f1;
int s2_f2;
int s2_f3;
} s2;
};
} test_struct_t;
//Works
test_struct_t t1 = {
.f1 = 0,
.f2 = 0,
.s1.s1_f1 = 0,
.s1.s1_f2 = 0,
.s1.s1_f3 = 0, };
//Works
test_struct_t t2 = {
0,
0,
{0,
0,
0,}
};
//Compilation error: union "<unnamed>" has no field "s1_f2"
test_struct_t t3 = {
.f1 = 0,
.f2 = 0,
.s1 = {
.s1_f1 = 0,
.s1_f2 = 0,
.s1_f3 = 0, }, };
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

  • Thank you for letting us know about this problem, and for submitting a concise test case.  I can reproduce the problem.  I filed CODEGEN-1523 in the SDOWP system to have this addressed.  You are welcome to follow it with the SDOWP link below in my signature.

    Thanks and regards,

    -George

  • Hello Vladimir, I had a chance to take a look at your issue and came to the conclusion that this is an enhancement that we could potentially support in the future.


    The feature which you are trying to use is a nonstandard extension added to the Plan9 and Microsoft C/C++ compilers, and implemented in GCC 4.6 and later. The extension states that designated initializers which name a field within a structure's anonymous union members are allowed.


    One workaround, which you've already discovered, is to initialize each field within the anonymous union, as in your first working example. Another is to identify that you're initializing the anonymous union via brace-wrapping. For your failing case, the following will work:

    test_struct_t t3 = {
        .f1 = 0,
        .f2 = 0,
        {
            .s1 = {
                .s1_f1 = 0,
                .s1_f2 = 0,
                .s1_f3 = 0,
            },
        }
    };


    Since this issue is a matter of supporting nonstandard extensions, I have marked the bug CODEGEN-1523 as rejected with a workaround.


    Thanks,

    J.B. Nagurne