簡體   English   中英

是否可以在 C 的結構內鍵入定義聯合

[英]Is it possible to typedef union inside a struct in C

我真的不明白工會是如何運作的。 有人可以解釋它是如何工作的嗎? 我可以 typedef 工會嗎? 如果答案是肯定的,我該怎么做? 下面這段代碼有什么問題?

typedef struct Car{
        int age;
        int weight;

        enum Type { Tesla, Lada } type;

        typedef union Consumption{
                double litre;
                int kwh;
        } Consumption;

        Consumption consumption;
} Car;

當我嘗試編譯此代碼時出現錯誤代碼:

union1.c:9:2: error: expected specifier-qualifier-list before ‘typedef’
  typedef union Consumption{
  ^~~~~~~

typedef不能存在於structunion中。 然而,這並不意味着您不能在另一個內部定義structunion 例如:

typedef struct Car{
        int age;
        int weight;

        enum Type { Tesla, Lada } type;

        union Consumption{
                double litre;
                int kwh;
        } consumption;
} Car;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM