Variable length array in the middle of struct - why this C code is valid for gcc

后端 未结 3 965
攒了一身酷
攒了一身酷 2020-12-09 16:23

There is some strange code using VLA (Variable Length Arrays) which is treated as Valid C (C99, C11) by gcc 4.6:

$ cat a.c
int main(int argc,char**argv)
{
           


        
3条回答
  •  执念已碎
    2020-12-09 17:05

    The standard is pretty clear that VLAs are not allowed in a struct:

    6.7.2.1 Structure and union specifiers

    9 - A member of a structure or union may have any complete object type other than a variably modified type. [...]

    Variably modified types are (as you might expect) those derived from a variable length array (e.g. by adding array dimensions or cv qualifiers):

    6.7.6 Declarators

    3 - [...] If, in the nested sequence of declarators in a full declarator, there is a declarator specifying a variable length array type, the type specified by the full declarator is said to be variably modified. Furthermore, any type derived by declarator type derivation from a variably modified type is itself variably modified.

提交回复
热议问题