Home > Software engineering >  Where is the usage of struct with flexible array memeber defined in the C/C standards?
Where is the usage of struct with flexible array memeber defined in the C/C standards?

Time:03-11

If I have code like this

struct s { int x; char b[]; };

int main() {
    struct s s[10];
}

and I compile with "gcc -O2 -W -Wall -pedantic" then I get:

<source>:4:14: warning: invalid use of structure with flexible array member [-Wpedantic]
    4 |     struct s s[10];
      |              ^

And gcc is totally right. Structs with flexible array members can't work like that.

Where in the C/C standards is this defined?

CodePudding user response:

In C, it's §6.7.2.1, paragraph 3:

A structure or union shall not contain a member with incomplete or function type,… except that the last member of a structure with more than one named member may have incomplete array type; such a structure (and any union containing, possibly recursively, a member that is such a structure) shall not be a member of a structure or an element of an array.

As @phuclv points out in a comment, C doesn't have this feature.

  • Related