【问题标题】:structure within union showing warning and some unpredictible output联合内的结构显示警告和一些不可预测的输出
【发布时间】:2016-09-13 09:30:53
【问题描述】:
    #include<stdio.h> 
    typedef union 
    {
           struct accname
           {
                   char sbi[20];
                   char canara[20];
                   char hdfc[20];
                   char icici[20];
           }; 
    } unionv; 

    void main()  
    {
           unionv var1;
           struct accname var2;
           printf("sizeof union=%d\n",sizeof(var1));
           printf("sizeof str=%d\n",sizeof(var2));
    }

这里显示警告。

warning: declaration does not declare anything [enabled by default]  }unionv

谁能清楚为什么它显示警告?

【问题讨论】:

  • 你为什么要声明一个只有一个成员的工会?

标签: c


【解决方案1】:

根据https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html#Unnamed-Fields

未命名字段必须是不带标签的structureunion 定义,例如,

typedef union 
{
     struct accname
     {
        //member      
     }; // without tag
} unionv; 

如果使用-fms-extensions,则该字段还可以是带有tag(例如struct foo { int a; };)的定义、对先前定义的structureunion(例如struct foo)的引用;或对先前定义的 structureunion 类型的 typedef 名称的引用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-28
    相关资源
    最近更新 更多