Why is this valid C

后端 未结 2 1863
无人共我
无人共我 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.

提交回复
热议问题