Anonymous union within struct not in c99?

后端 未结 7 1057
时光说笑
时光说笑 2020-11-28 11:40

here is very simplified code of problem I have:

enum node_type {
    t_int, t_double
};

struct int_node {
    int value;
};

struct double_node {
    double valu         


        
7条回答
  •  清歌不尽
    2020-11-28 12:05

    Well, the solution was to name instance of the union (which can remain anonymous as datatype) and then use that name as a proxy.

    $ diff -u old_us.c us.c 
    --- old_us.c    2010-07-12 13:49:25.000000000 +0200
    +++ us.c        2010-07-12 13:49:02.000000000 +0200
    @@ -15,7 +15,7 @@
       union {
         struct int_node int_n;
         struct double_node double_n;
    -  };
    +  } data;
     };
    
     int main(void) {
    @@ -23,6 +23,6 @@
       i.value = 10;
       struct node n;
       n.type = t_int;
    -  n.int_n = i;
    +  n.data.int_n = i;
       return 0;
     }
    

    Now it compiles as c99 without any problems.

    $ cc -std=c99 us.c 
    $ 
    

    Note: I am not happy about this solution anyway.

提交回复
热议问题