### Structs - is it possible>?

Hi,

I need to create a struct to hold an array of values.

I would normally do this...

 ``12345`` ``````struct myList { int myvalues[]; }``````

and populate it like this....

 ``12345678910`` ``````myList tmp; tmp.someAddCode(1); tmp.someAddCode(2); tmp.someAddCode(3); tmp.someAddCode(4); tmp.someAddCode(5); tmp.someAddCode(6); tmp.someAddCode(7); tmp.someAddCode(8); tmp.someAddCode(9);``````

is it possible to do some code which would allow me to initialise it like this instead?

 `` `` ``myList tmp = {1, 2, 3, 4, 5, 6, 7, 8, 9} //undefined amount of items ``

Thanks :)
In C++11, yes.

In the older C++/C, you can't use that syntax. You could probably hack around it though...the first way would be the easiest to do though.
 ``12345`` ``````struct myList { myList(std::initializer_list vals) : myValues(vals) {} std::vector myValues; };``````

With an extra pair of braces, you don't even need a constructor:
`myList tmp = {{1, 2, 3, 4, 5, 6, 7, 8, 9}};`
Last edited on
 ``123456`` ``````struct myList { int myvaluesA[][]; float myvaluesB[][]; } ``````

can I do this, then?

 ``123`` `````` myList = {{{1, 2, 3}, {7, 8, 9}} , {{1.1, 2.1, 3.1}, {7.1, 8.1, 9.1}}} ``````

would be cool if i could....
If you turn the int/float arrays into std::vectors, yeah, it'll work for C++11.
i dont wanna use std:vectors....
And why is that?
What version of c++ supports this (non array) technique, please?

 ``123456789`` ``````struct something { int someint; int someint2; int someint3; int someint4; float somefloat; } something tmp = {1, 2, 3, 4, 2.3}``````

i am trying to write really compact code that will do on microprocessors... i am still to see if the processors are likely to be running c++11
 What version of c++ supports this (non array) technique, please?

All versions. Even C supports it.

 i am trying to write really compact code that will do on microprocessors...

You can't use dynamic structures without using dynamic structures, so you can't get much more compact than vector.

 i am still to see if the processors are likely to be running c++11

That depends more on the compiler than the hardware.
thanks
Topic archived. No new replies allowed.