The C99 Standard at 6.7.2.1/18 says the following is invalid
struct s { int x; int d[]; } e = { 0, { 1, 2 } };
"because struct s is treated as if it did not contain member d.". I
understand that this is just non-normative example text, so I was looking
for normative text about it. 6.7.8 about initialization does not state that
FAMs are ignored. Where is such a thing stated? 6.7.2.1/16 says "In most
situations, the flexible array member is ignored.", which doesn't forbid the
initialization either.
No, but it then goes on to say something else that causes serious problems:
"the size of the structure is as if the flexible array member were
omitted except that it may have more trailing padding than the omission
would imply."
Later on, it says: "when a . (or ->) operator has a left operand that is
(a pointer to) a structure with a flexible array member and the right
operand names that member, it behaves as if that member were replaced
with the longest array (with the same element type) that would not make
the structure larger than the object being accessed; the offset of the
array shall remain that of the flexible array member, even if this would
differ from that of the replacement array. If this array would have no
elements, it behaves as if it had one element but the behavior is
undefined if any attempt is made to access that element or to generate a
pointer one past it."
The consequence of this is that, since the compiler only sets aside
sizeof(struct s) bytes for e, then unless that size includes a
sufficient amount of padding, the replacement array will have zero
elements, which would mean that there is no such object as e.d[0] or
e.d[1]. In that case, your initializer violates the constraint in
6.7.8p1: "No initializer shall attempt to provide a value for an object
not contained within the entity being initialized."
That's why flexible array members are normally dynamically allocated. In
that case, the relevant object is the entire block of memory that was
allocated, which can (with care) be arranged to be big enough for
everything to work right.