From C99 you can also initialize only the subobjects you need and the rest will be initialized as data with static storage duration (0
for int
):
int values[10] =
{
[0] = 197,
[2] = -100,
[5] = 350,
[3] = values[0] + values[5],
[9] = values[5] / 10
};
More complicated initializations are impossible/unclear to do this way. Things like:
--values[2];
Cannot be done in the initialization because if there are more initializers for the same element, only the last will be used and the first ones could not even be evaluated, causing a decrement on an uninitialized value. From C11 standard:
The initialization shall occur in initializer list order, each initializer provided for a
particular subobject overriding any previously listed initializer for the same subobject;
151)
151) Any initializer for the subobject which is overridden and so not used to initialize that subobject might not be evaluated at all.
(I think it deserves more than a foot-note...)
However, I think that's just an example since there is no point to do so, you can initialize it to 350 - 1
in this case.