int a[6] = {[4] = 7}; is compiled by a C99 compiler. What are the values of a[0] and a[4]?
Correct answer
D. a[0] is 0 and a[4] is 7, since omitted elements are initialised as if static
15 views
D. a[0] is 0 and a[4] is 7, since omitted elements are initialised as if static
15 views
Designated initialisers set the named element and leave the rest zero, because a partially initialised aggregate is zero-filled first. Writing int a[6] = {[4] = 7} therefore yields {0,0,0,0,7,0}. C++20 accepts the same syntax but requires designators to appear in declaration order, unlike C where any order is allowed and later designators overwrite earlier ones.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is D? Ask here and get an explanation.