Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to assign anonymous VLA to pointer?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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).

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading