declaration/initialization of multidimensional arrays

Tyro[17] nospam at home.com
Sun Nov 18 16:47:16 PST 2012


What is the proper way to declare a multidimensional array?
In Java, I can do:

	double[][] a = new double[M][N];

D does not allow this because of two reasons.

1) M & N are variables and cannot be read at compile time even if they 
are explicitly initialized.

	Error: variable N cannot be read at compile time (sawalks)

2) double[17][17] is a static array and cannot be used to initialize a 
dynamic array.

	Error: cannot implicitly convert expression (new double[17LU][](17LU)) 
of type double[17LU][] to double[][] (sawalks)

Assigning a length and then trying to access the values obviously will 
not work:

	double[][] a;
	a.length = M * N;
	a[i][j] = 17; // core.exception.RangeError at sawalks(19): Range violation

So what is the proper way to do this in D?

Thanks


More information about the Digitalmars-d-learn mailing list