C99: flexible array inside connection?

I tried converting something from using struct hack to using a flexible array element, only to execute the following error message:

error: invalid use of structure with flexible array element

(GCC 4.8.1, gnu99, MinGW)

After trying to track down the cause of the message, I redid it into the following relatively minimal case:

struct a { union { struct { int b; int c[]; } d; } e; }; 

In other words, a structure with a flexible member of an array does not see to be placed inside the union in the structure, even if the union is the last member of the structure.

(Note that including the flex array element directly inside the union does work.)

Now: is there a good way to get around this, besides returning back to the structure hacker (declaring c as an array of length 1)? A pointer to the structure within the union will work, but an extra layer of indirection suffers.

+6
source share
1 answer

The C11 standard (ISO / IEC 9899: 2011) states:

Β§6.7.2.1 Structure and Association Specifications

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

In the example you have, the last member of struct a not a flexible member of the array. This is a union containing a struct that has a flexible member of an array.

You need to work hard to get gcc to complain; it requires -pedantic among the compiler options.

+3
source

Source: https://habr.com/ru/post/970545/


All Articles