联合会支持灵活的数组成员吗? [英] Does union support flexible array members?

查看:90
本文介绍了联合会支持灵活的数组成员吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经声明了一个工会中的灵活数组成员,像这样:

I have declared a flexible array member in union, like this:

#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[];

但是,声明数组的大小是这样的:

But, Declared array zero size like this :

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

工作正常。

为什么零长度数组可以很好地结合?

推荐答案

否,联合不支持灵活的数组成员,仅支持结构。 C11 6.7.2.1§18

No, unions do not support flexible array members, only structs. C11 6.7.2.1 §18


作为特殊情况,结构中最后一个元素中有一个以上名为
的成员数组类型不完整;这称为
柔性数组成员

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

In addition, zero-length arrays is not valid C, that's a gcc non-standard extension. The reason why you get this to work is because your compiler, gcc, is configured to compile code for the "non-standard GNU language". If you would rather have it compile code for the C programming language, you need to add compiler options -std=c11 -pedantic-errors.

这篇关于联合会支持灵活的数组成员吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆