How is it legal to reference an undefined type inside a structure?

后端 未结 7 1437
-上瘾入骨i
-上瘾入骨i 2020-12-20 15:52

As part of answering another question, I came across a piece of code like this, which gcc compiles without complaint.

typedef struct {
    struct xyz *z;
} x         


        
7条回答
  •  一生所求
    2020-12-20 16:25

    The parts of the C99 standard you are after are 6.7.2.3, paragraph 7:

    If a type specifier of the form struct-or-union identifier occurs other than as part of one of the above forms, and no other declaration of the identifier as a tag is visible, then it declares an incomplete structure or union type, and declares the identifier as the tag of that type.

    ...and 6.2.5 paragraph 22:

    A structure or union type of unknown content (as described in 6.7.2.3) is an incomplete type. It is completed, for all declarations of that type, by declaring the same structure or union tag with its defining content later in the same scope.

提交回复
热议问题