A question on C w.r.t arrays | Sololearn: Learn to code for FREE!
+ 4

A question on C w.r.t arrays

How does the code below not cause a compilation error? int a[50]={0,1,4,[47]=47,48,49,50};

6th Feb 2019, 3:25 PM
Ajeya Bhat
Ajeya Bhat - avatar
1 Answer
+ 2
Bennett Post Amazing. I have never seen nor read about this before. Is declaration order the only difference between C and C++ in this topic? Copied verbatim from the eel.is link: "designators for array elements and nested designators are not supported". Does this imply that the code posted by Ajeya is only valid in C and not C++? I've just tested a few cases with C++. int a[2] = {[1]=1}; // does not compile int a[2] = {0, [1]=1}; // works fine int a[2] = {[0]=1}; // works fine Does this mean that all elements prior to the designator has to be initialized? Doesn't this defeat the purpose of using them?
7th Feb 2019, 5:31 AM
Hatsy Rei
Hatsy Rei - avatar