initializing static rectangular arrays

llee llee at goucher.edu
Sat Nov 17 16:32:23 PST 2007


naryl Wrote:

> On Sun, 18 Nov 2007 00:54:31 +0300, llee <llee at goucher.edu> wrote:
> 
> > Janice Caron Wrote:
> >
> >> On 11/17/07, llee <llee at goucher.edu> wrote:
> >> > how do you initialize a static multidimensional array? I tried the  
> >> following, but it did not work.
> >> >
> >> > float[3][4] array =
> >> > [
> >> >    [1.1, 1.2, 1.3],
> >> >    [2.1, 2.2, 2.3],
> >> >    [3.1, 3.2, 3.3]
> >> > ];
> >>
> >> Isn't that a 3x3 array, not a 3x4 array?
> >>
> >> Anyway, I think the solution to your dilemma is the keyword "const".
> >> (Or if that doesn't work, try "static").
> >
> > didn't work. I tried:
> >
> > 	const float[3][4] elements =
> > 	[
> > 		[1.1, 1.2, 1.3, 1.4],
> > 		[2.1, 2.2, 2.3, 2.4],
> > 		[3.1, 3.2, 3.3, 3.4]		
> > 	];
> >
> > and the compiler returned "too many initializers" for each nested array.
> 
> float[3][4] is really an array of 4 arrays of 3 floats.  
> http://www.digitalmars.com/d/1.0/arrays.html
> 
> You can initialize it like that:
> 	float[3][4] elements =
>          [
>   	    [1.1, 1.2, 1.3],
>              [2.1, 2.2, 2.3],
>              [3.1, 3.2, 3.3],
>              [4.1, 4.2, 4.3]
>          ];

thanks for the help. The following worked.

	const float[3][4] elements =
	[
		[1.1, 1.2, 1.3],
		[2.1, 2.2, 2.3],
		[3.1, 3.2, 3.3],
		[4.1, 4.2, 4.3]
	];




More information about the Digitalmars-d mailing list