加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – union是否支持灵活的阵列成员?

发布时间:2020-12-16 10:14:17 所属栏目:百科 来源:网络整理
导读:我在联合中声明了一个灵活的数组成员,如下所示: #include stdio.hunion ut{ int i; int a[]; // flexible array member};int main(void){ union ut s; return 0;} 和编译器给出一个错误: source_file.c:8:9: error: flexible array member in union int a[
我在联合中声明了一个灵活的数组成员,如下所示:

#include  <stdio.h>

union ut
{
    int i;
    int a[]; // flexible array member
};

int main(void)
{
    union ut s;
    return 0;
}

和编译器给出一个错误:

source_file.c:8:9: error: flexible array member in union
     int a[];

但是,声明的数组零大小如下:

union ut
{
    int i;
    int a[0]; // Zero length array
};

它工作正常.

为什么零长度阵列工作精细?

解决方法

不,工会不支持灵活的阵列成员,只支持结构. C116.7.2.1§18

As a special case,the last element of a structure with more than one
named member may have an incomplete array type; this is called a
flexible array member.

此外,零长度数组无效C,这是gcc非标准扩展.之所以能够使用它是因为你的编译器gcc被配置为编译“非标准GNU语言”的代码.如果您希望编译C编程语言的代码,则需要添加编译器选项-std = c11 -pedantic-errors.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读