Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline initialization of an array

I have this struct:

struct Foo {
    int a;
    int* b;
};

Then I'm creating an instance for it like this:

int x [] = { 5, 6 };
Foo y = { 2, x };

But, I'd like to create the x array inline, maybe something like this:

struct Foo y = { 2, (int[]) { 5, 6 } };

But the example above do not work... How can I achieve this?

--------- EDIT:

I'm getting this error from intellisense:

cast to incomplete array type "int []" is not allowed

Build error:

Error C4576 a parenthesized type followed by an initializer list is a non-standard explicit type conversion syntax

I'm using Visual Studio 2015 (v140).

like image 528
João Paulo Avatar asked Jul 14 '26 12:07

João Paulo


1 Answers

In your case, Foo is not a type.

Try

struct Foo y = { 2, (int[]) { 5, 6 } };

It works as expected


Edit:

You only need to free() the memory programatically which you allocate using the allocator function (malloc() and family). The memory which is not returned by an allocator function, need not be free()-d by the programmer.


Edit 1:

Regarding the C4567, see this

like image 118
Sourav Ghosh Avatar answered Jul 16 '26 02:07

Sourav Ghosh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!