Initialise dynamic array in array of structures

cym13 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 21 12:33:31 PDT 2016


On Tuesday, 21 June 2016 at 19:15:56 UTC, Paul wrote:
> Given these structures and declaration:
>
> struct CoordList{
>         int x, y;
> }
>
> struct Part{
> 	int x, y;
> 	CoordList[] coords;
> 	int nextNode, prevNode;
> 	string nextEnter, prevEnter;
> }
>
> Part[10] trackTemplates;
>
>
> Can someone please tell me why I can't initialise 
> Part[0].coords like this:
>
> trackTemplates[0].coords = [ {0, 9}, {1, 1}, {3, 6} ];
>
> but I can do this:
>
> CoordList[] temp = [ {0, 9}, {1, 1}, {3, 6} ];
> trackTemplates[0].coords = temp;
>
> Many thanks!

My take is that “CoordList[] temp = [{0, 9}, {1, 1}, {3, 6}];” is 
initialization because you're setting the value when defining it 
but “trackTemplates[0].coords = [{0, 9}, {1, 1}, {3, 6}];” is an 
assignment so the compiler can infer as much and doesn't 
understand that each of those list of values are really 
CoordLists. For example “    trackTemplates[0].coords = 
[CoordList(0, 9), CoordList(1, 1), CoordList(3, 6)];” would have 
worked as expected.



More information about the Digitalmars-d-learn mailing list