Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Where is the usage of struct with flexible array memeber defined in the C/C++ standards?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Where in the C/C++ standards is this defined?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading