Why is this valid C

后端 未结 2 1854
无人共我
无人共我 2020-12-29 17:54

I came across this code on reddit. I would have thought that type conversions would have caused this to be invalid.

int a[3] = { { {1, 2}, {3, 4}, 5, 6 }, {7         


        
相关标签:
2条回答
  • 2020-12-29 18:39
      int a[3] = { { {1, 2}, {3, 4}, 5, 6 }, {7, 8}, {9}, 10 };
    

    is invalid.

    It is invalid for the same reasons int b[1] = {1, 2}; is invalid: because C99 says

    (C99, 6.7.8p1) "No initializer shall attempt to provide a value for an object not contained within the entity being initialized."

    The last element 10 in a initializers attempts to provide a value for an object not contained within the entity being initialized.

    0 讨论(0)
  • 2020-12-29 18:41

    The excess elements are just ignored. There are two parts of 6.7.8 Initialization that you care about. First, from paragraph 17:

    Each brace-enclosed initializer list has an associated current object. When no designations are present, subobjects of the current object are initialized in order according to the type of the current object: array elements in increasing subscript order, structure members in declaration order, and the first named member of a union.

    That one explains why you get 1, 7, and 9 - the current object gets set by those braces. Then as to why it doesn't care about the extras, from paragraph 20:

    ... only enough initializers from the list are taken to account for the elements or members of the subaggregate or the first member of the contained union; any remaining initializers are left to initialize the next element or member of the aggregate of which the current subaggregate or contained union is a part.

    0 讨论(0)
提交回复
热议问题