Currently I am assigning a VLA to a pointer as follows
struct Foo {
int* array;
};
int array[size];
struct Foo foo = {
.array = array;
};
Is it possible to replace this with an "anonymous" array?
What I tried:
struct Foo foo = {
.array = (int[size]) {} // fatal error: variable-sized object may not be initialized
};
What I was hoping for:
struct Foo foo = {
.array = int[size]; // something similar to this if I am making any sense.
};
PS: I am not looking for dynamic memory allocation (malloc/calloc).
Is it possible to replace this with an "anonymous" array?
No.
Specifically, you are asking how to create an anonymous variable-length array. C does not support those.
Of compound literals ((){}
), the C standard says:
C17 §6.5.2.5 ¶1 The type name shall specify a complete object type or an array of unknown size, but not a variable length array type.