www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - initializing static rectangular arrays

reply llee <llee goucher.edu> writes:
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]
];
Nov 17 2007
next sibling parent reply "Janice Caron" <caron800 googlemail.com> writes:
On 11/17/07, llee <llee 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").
Nov 17 2007
parent reply llee <llee goucher.edu> writes:
Janice Caron Wrote:

 On 11/17/07, llee <llee 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.
Nov 17 2007
parent llee <llee goucher.edu> writes:
naryl Wrote:

 On Sun, 18 Nov 2007 00:54:31 +0300, llee <llee goucher.edu> wrote:
 
 Janice Caron Wrote:

 On 11/17/07, llee <llee goucher.edu> wrote:
 how do you initialize a static multidimensional array? I tried the  

 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] ];
Nov 17 2007
prev sibling next sibling parent naryl <cy ngs.ru> writes:
On Sun, 18 Nov 2007 00:54:31 +0300, llee <llee goucher.edu> wrote:

 Janice Caron Wrote:

 On 11/17/07, llee <llee goucher.edu> wrote:
 how do you initialize a static multidimensional array? I tried the =



 following, but it did not work.
 float[3][4] array =3D
 [
    [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 =3D [ [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 arra=

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 =3D [ [1.1, 1.2, 1.3], [2.1, 2.2, 2.3], [3.1, 3.2, 3.3], [4.1, 4.2, 4.3] ];
Nov 17 2007
prev sibling parent "Janice Caron" <caron800 googlemail.com> writes:
On 11/17/07, naryl <cy ngs.ru> wrote:
 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]
          ];

Yes, because D puts the array dimension on the type, not the variable. (But you can do it the C way if you want). So float elements[4][3]; == float[3] elements[4]; == float[3][4] elements; (At least, I think that's right. Disclaimer: I don't have a compiler handy right now).
Nov 17 2007