cpointersstructlanguage-lawyerincomplete-type

Does the C standard require the first operand of the -> operator to be pointer to complete structure?


Consider this code:

struct s
{
    int x;
    int y[sizeof((struct s*)0)->x];
};

The C23 has the following constraint (6.5.3.4p2):

The first operand of the -> operator shall have type "pointer to atomic, qualified, or unqualified structure" or "pointer to atomic, qualified, or unqualified union", and the second operand shall name a member of the type pointed to.

Here I don't see a requirement for such "pointer to atomic, qualified, or unqualified structure" to be pointer to atomic, qualified, or unqualified complete structure.

The example above seems to satisfy the constraint above. Why do major C compilers reject this code?


In this comment user Caleth thinks that "the lack of explicit requirement for -> to apply to complete types is an omission on the part of the C committee".


UPD. SDCC 4.5.0 accepts the code above.


Solution

  • I think it is correct to assume that this is a miss from the C committee.

    For example with C11 the text "pointer to complete object type" was added to other postfix operator constraints like the [] operator. C99 didn't have that "complete" requirement. They didn't add a similar wording to -> and . for whatever the reason.

    As noted in comments I don't really see how the second operand can name a member if the structure is incomplete though.