Language explicitly prohibits this
6.5.2.5 Compound literals
Limitations
1 The type name should indicate the type of the object or an array of unknown size, but not the type of the array of variable length.
If you need something like this, you will have to use a VLA object with a name, not a compilation literal. However, note that VLA types do not accept initializers, which means you cannot do this.
char buf[len] = { 0 }; // ERROR for non-constant `len`
(I have no idea what the rationale for this restriction is.)
So, in addition to using the named VLA object, you will have to come up with some way to nullify it, for example, a memset or an explicit loop.
source share