Does union support flexible array members?
Asked Answered
D

3

8

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;
}

and compiler gives an error :

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
};

And it's working fine.

Why does zero length array work fine union?

Darnelldarner answered 15/9, 2017 at 6:49 Comment(4)
Show your compile command.Never
Unions are more or less an amalgamation of types at compile-time. A flexible array member is really a run-time thing.Antisepticize
remove gnu support, and the second one will also throw error.Gladiator
"Working fine"...wait..what? You mean compile?Gladiator
E
9

No, unions do not support flexible array members, only structs. C11 6.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.

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.

Eggleston answered 15/9, 2017 at 6:56 Comment(0)
N
8

int a[] is the C standard notation (since C99).

int a[0] is GNU C syntax, which predates C99. Other compilers might also support it, I don't know.

Your compiler seems to default to C90 standard with GNU extensions, which is why latter compiles, but first one does.

Furthermore, as stated in Lundin's answer, standard C does not support flexible array members in union at all.


Try adding -std=c99 or -std=c11 to your compiler options (gcc docs here).

Also -pedantic or -pedantic-errors is probably a good idea too, it'll enforce more strict standard compliance.

And, obligatory aside, -Wall -Wextra won't hurt either...

Never answered 15/9, 2017 at 6:53 Comment(4)
also -pedantic, maybe?Gladiator
A compiler accepting int a[0] is not compliant, since zero-length arrays are explicitly forbidden in C.Eggleston
GCC is not compliant without -pedantic.Gonion
@Lundin: A Conforming C Implementation may accept and usefully process such a construct provided it issues at least one diagnostic, and a Conforming C Program (but not Strictly Conforming C Program) may make use of such a construct provided that there exists somewhere in the universe a Conforming C Implementation that accepts it.Yowl
D
0

I'm not sure what the standard would say about this, but G++' unions seems to accept flexible arrays just fine. If you wrap them in an anonymous struct first, like so:

union {
   unsigned long int  ul;
   char  fixed[4][2];
   struct {
      char  flexible[][2];
   };
};
Dupin answered 13/2, 2020 at 1:5 Comment(1)
Doesn't work these days if you compile with standard options like -std=c11, giving error: flexible array member in a struct with no named members.Gemmiparous

© 2022 - 2024 — McMap. All rights reserved.