www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How initialize a static rectangular array (2-d matrix)

reply Justin Johansson <no spam.com> writes:
I almost have to apologize for this question but ..

How does one initialize a static rectangular array (2-d matrix) in D1?

None of the following or other variations that I've tried compile with DMD 1.0.

int[2, 3] matrix = [ 1, 2, 3,  4, 5, 6 ]; 

int[2, 3] matrix = [[ 1, 2, 3 ], [ 4, 5, 6 ]]; 

Thanks for all help.
Oct 08 2009
parent reply Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Thu, Oct 8, 2009 at 8:10 PM, Justin Johansson <no spam.com> wrote:
 I almost have to apologize for this question but ..

 How does one initialize a static rectangular array (2-d matrix) in D1?

 None of the following or other variations that I've tried compile with DM=
D 1.0.
 int[2, 3] matrix =3D [ 1, 2, 3, =A04, 5, 6 ];
..that's not how you declare a rectangular array type. It's int[3][2]. int[3][2] matrix =3D [[1, 2, 3], [4, 5, 6]];
Oct 08 2009
next sibling parent Justin Johansson <no spam.com> writes:
Jarrett Billingsley Wrote:

 On Thu, Oct 8, 2009 at 8:10 PM, Justin Johansson <no spam.com> wrote:
 I almost have to apologize for this question but ..

 How does one initialize a static rectangular array (2-d matrix) in D1?

 None of the following or other variations that I've tried compile with DMD 1.0.

 int[2, 3] matrix = [ 1, 2, 3,  4, 5, 6 ];
..that's not how you declare a rectangular array type. It's int[3][2]. int[3][2] matrix = [[1, 2, 3], [4, 5, 6]];
Of course. Blonde moment. Worked 18 hrs yesterday & not enough sleep. :-(
Oct 08 2009
prev sibling parent reply Justin Johansson <no spam.com> writes:
Jarrett Billingsley Wrote:

 On Thu, Oct 8, 2009 at 8:10 PM, Justin Johansson <no spam.com> wrote:
 I almost have to apologize for this question but ..

 How does one initialize a static rectangular array (2-d matrix) in D1?

 None of the following or other variations that I've tried compile with DMD 1.0.

 int[2, 3] matrix = [ 1, 2, 3,  4, 5, 6 ];
..that's not how you declare a rectangular array type. It's int[3][2]. int[3][2] matrix = [[1, 2, 3], [4, 5, 6]];
Thanks Jarrett. Am I having a blonde day? int[2,3] m = [ 1, 2, 3 ]; writefln( "m=%s, .sizeof=%d", m, m.sizeof); Compiles and prints: m=[1,2,3], .sizeof=12 So what does that declaration, int[2,3], mean? Think this is what threw me initially. Thanks, Justin
Oct 08 2009
next sibling parent reply Christopher Wright <dhasenan gmail.com> writes:
Justin Johansson wrote:
 Jarrett Billingsley Wrote:
 
 On Thu, Oct 8, 2009 at 8:10 PM, Justin Johansson <no spam.com> wrote:
 I almost have to apologize for this question but ..

 How does one initialize a static rectangular array (2-d matrix) in D1?

 None of the following or other variations that I've tried compile with DMD 1.0.

 int[2, 3] matrix = [ 1, 2, 3,  4, 5, 6 ];
..that's not how you declare a rectangular array type. It's int[3][2]. int[3][2] matrix = [[1, 2, 3], [4, 5, 6]];
Thanks Jarrett. Am I having a blonde day? int[2,3] m = [ 1, 2, 3 ]; writefln( "m=%s, .sizeof=%d", m, m.sizeof); Compiles and prints: m=[1,2,3], .sizeof=12 So what does that declaration, int[2,3], mean? Think this is what threw me initially. Thanks, Justin
2,3 is a comma expression. Evaluate everything, and return the rightmost value.
Oct 08 2009
parent reply Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Thu, Oct 8, 2009 at 10:05 PM, Christopher Wright <dhasenan gmail.com> wr=
ote:
 Justin Johansson wrote:
 Jarrett Billingsley Wrote:

 On Thu, Oct 8, 2009 at 8:10 PM, Justin Johansson <no spam.com> wrote:
 I almost have to apologize for this question but ..

 How does one initialize a static rectangular array (2-d matrix) in D1?

 None of the following or other variations that I've tried compile with
 DMD 1.0.

 int[2, 3] matrix =3D [ 1, 2, 3, =A04, 5, 6 ];
..that's not how you declare a rectangular array type. It's int[3][2]. int[3][2] matrix =3D [[1, 2, 3], [4, 5, 6]];
Thanks Jarrett. Am I having a blonde day? =A0 int[2,3] m =3D [ 1, 2, 3 ]; =A0 writefln( "m=3D%s, .sizeof=3D%d", m,=
m.sizeof);
 Compiles and prints:

 m=3D[1,2,3], .sizeof=3D12

 So what does that declaration, int[2,3], mean?

 Think this is what threw me initially.

 Thanks, Justin
2,3 is a comma expression. Evaluate everything, and return the rightmost value.
Bahahaha, oh comma exps.
Oct 08 2009
parent bearophile <bearophileHUGS lycos.com> writes:
Jarrett Billingsley Wrote:

 Bahahaha, oh comma exps.
Their usage will need to be quite regulated in D (using them for smarter purposes is a cute idea, but it may impair C compatibility). Bye, bearophile
Oct 08 2009
prev sibling parent Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
Justin Johansson wrote:
 Jarrett Billingsley Wrote:
 
 On Thu, Oct 8, 2009 at 8:10 PM, Justin Johansson <no spam.com> wrote:
Thanks Jarrett. Am I having a blonde day? int[2,3] m = [ 1, 2, 3 ]; writefln( "m=%s, .sizeof=%d", m, m.sizeof); Compiles and prints: m=[1,2,3], .sizeof=12 So what does that declaration, int[2,3], mean? Think this is what threw me initially. Thanks, Justin
It probably means DMD isn't doing enough validation checking. Comma expression, hence typeof(m) is int[3], m.sizeof is 3 * int.sizeof
Oct 08 2009