Strange values while initializing array using designated initializers

前端 未结 5 1077
迷失自我
迷失自我 2021-02-07 05:57

When I initialize the array below all the output looks ok except for values[3]. For some reason values[3] initialized as values[0]+values[5]

5条回答
  •  心在旅途
    2021-02-07 06:11

    int values[10] = { 
        [0]=197,[2]=-100,[5]=350,
        [3]=values[0] + values[5],
        [9]= values[5]/10
    };
    

    edit:

    The ISO C99 standard, section 6.7.8 (Initialization) specifies that

    The initialization shall occur in initializer list order, each initializer provided for a particular subobject overriding any previously listed initializer for the same subobject;132) all subobjects that are not initialized explicitly shall be initialized implicitly the same as objects that have static storage duration

    But as Shafik pointed out, the evaluation order doesnt have to match the initialization order

    Which means values[0] + values[5] may read garbage values from:

    • values[0]
    • values[5] (this is what happen in your case)
    • both
    • none of them

提交回复
热议问题