Creating an array of user-defined structs

brian via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Aug 21 16:28:14 PDT 2016


So I have the below program:

import std.stdio;

/* defines my awesome struct */
struct testStruct
{
	string aa;
	string bb;
}

/* creates(?) an empty array of structs */
testStruct[int] testStructArray;

void main()
{	
	string a = "a";
	string b = "b";

/*make a new struct */
	auto newStruct = new testStruct;
/* populate the values for the struct */
	newStruct.aa = a.dup;
	newStruct.bb = b.dup;

	writeln(newStruct.aa);
/* oddly if I remove the write to "bb" line, the aa line gives an 
error above user access */
	writeln(newStruct.bb);

/* this errors, claiming something about a pointer?? */
	testStructArray[1] = newStruct;
}

Running this my output says:
Error: cannot implicitly convert expression (newStruct) of type 
testStruct* to testStruct

So I have a couple of questions:
1) I'm assuming by the error that line 23 defines a pointer 
rather than a ... not-pointer thing. I'm not sure why it is doing 
that, and would like some explanation please. :)
2) Is this the best way to do what I am trying to do, which is 
dynamically grow an array of user defined structs?

TIA
Brian


More information about the Digitalmars-d-learn mailing list