www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Constructing arrays of structs

reply Stephen Tashiro <tashiro zianet.com> writes:
If the constructor of a class needs to create an array of structs 
whose dimensions are inputs, what's the syntax for doing this?

For a non-example, the following program errors in main() because 
in t.array[][] "index [0] is out of bounds".


import std.stdio;

     struct Point
     {
      uint x;
      uint y;
      void printInfo()
      {
         printf("(%d $d )",x,y);
      }
     }


     class testClass
     {
       uint dimension;
       Point[][] array;

       this(uint the_dimension)
       {
         dimension = the_dimension;
         auto array = new Point[][](the_dimension,the_dimension);
         for(uint i = 0; i < dimension; i++)
         {
            for(uint j = 0; j < dimension; j++)
            {
               array[i][j].x = i;
               array[i][j].y = j;
            }
         }
       }
     }

     void main()
     {
        auto t = new testClass(5);

        for(uint i = 0; i < t.dimension; i++)
        {
           for(uint j = 0; j < t.dimension; j++)
           {
             printf("(%d %d)",t.array[i][j].x, t.array[i][j].y);
              //t.array[i][j].printInfo();
           }

        }

     }
Jan 23
next sibling parent reply Renato <renato athaydes.com> writes:
On Tuesday, 23 January 2024 at 18:15:29 UTC, Stephen Tashiro 
wrote:
 If the constructor of a class needs to create an array of 
 structs whose dimensions are inputs, what's the syntax for 
 doing this?

 For a non-example, the following program errors in main() 
 because in t.array[][] "index [0] is out of bounds".


 import std.stdio;

     struct Point
     {
      uint x;
      uint y;
      void printInfo()
      {
         printf("(%d $d )",x,y);
      }
     }


     class testClass
     {
       uint dimension;
       Point[][] array;

       this(uint the_dimension)
       {
         dimension = the_dimension;
         auto array = new Point[][](the_dimension,the_dimension);
         for(uint i = 0; i < dimension; i++)
         {
            for(uint j = 0; j < dimension; j++)
            {
               array[i][j].x = i;
               array[i][j].y = j;
            }
         }
       }
     }

     void main()
     {
        auto t = new testClass(5);

        for(uint i = 0; i < t.dimension; i++)
        {
           for(uint j = 0; j < t.dimension; j++)
           {
             printf("(%d %d)",t.array[i][j].x, t.array[i][j].y);
              //t.array[i][j].printInfo();
           }

        }

     }
This works , your mistake was to not actually assign the array to the class' field! Change this line: ```d auto array = new Point[][](the_dimension,the_dimension); ``` To this: ```d this.array = new Point[][](the_dimension,the_dimension); ```
Jan 23
parent reply Stephen Tashiro <tashiro zianet.com> writes:
On Tuesday, 23 January 2024 at 18:23:22 UTC, Renato wrote:
 This works , your mistake was to not actually assign the array 
 to the class' field!

 Change this line:

 ```d
 auto array = new Point[][](the_dimension,the_dimension);
 ```

 To this:

 ```d
 this.array = new Point[][](the_dimension,the_dimension);
 ```
Thank you. I don't really understand what the syntax new Point[][](the_dimension,the_dimension); denotes. Does it represent a function? To look up this topic, what are the proper keywords? By experimentation, I found that "new Point[the_dimension][the_dimension];" doesn't compile.
Jan 23
next sibling parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Tuesday, January 23, 2024 12:32:31 PM MST Stephen Tashiro via Digitalmars-
d-learn wrote:
 On Tuesday, 23 January 2024 at 18:23:22 UTC, Renato wrote:
 This works , your mistake was to not actually assign the array
 to the class' field!

 Change this line:

 ```d
 auto array = new Point[][](the_dimension,the_dimension);
 ```

 To this:

 ```d
 this.array = new Point[][](the_dimension,the_dimension);
 ```
Thank you. I don't really understand what the syntax new Point[][](the_dimension,the_dimension); denotes. Does it represent a function? To look up this topic, what are the proper keywords? By experimentation, I found that "new Point[the_dimension][the_dimension];" doesn't compile.
Except for the first dimension, all subsequent dimensions have to go in the parens, since putting them between the brackets indicates that that dimension is for a static array rather than a dynamic array, so it would change the type of the array (allowing for dynamic arrays of static arrays). As it is, you can probably only put the first dimension between the brackets, because other languages do that, and allowing it makes it easier to port code. Arguably though, for consistency, you should always put the dimensions between the parens when allocating a new dynamic array. - Jonathan M Davis
Jan 23
prev sibling parent Renato <renato athaydes.com> writes:
On Tuesday, 23 January 2024 at 19:32:31 UTC, Stephen Tashiro 
wrote:
 Thank you.

 I don't really understand what the syntax

 new Point[][](the_dimension,the_dimension);

  denotes. Does it represent a function?  To look up this topic, 
 what are the proper keywords?

 By experimentation, I found that "new 
 Point[the_dimension][the_dimension];" doesn't compile.
This is how you create a _multidimensional array_ in D that's allocated on the heap. The wiki mentions this: https://wiki.dlang.org/Dense_multidimensional_arrays You could also create a "static array" (on the stack, not heap): ```d import std.stdio; void main() { // allocate on the heap int[][] matrix = new int[][](5, 2); writeln(matrix); // allocate on the stack (I don't actually know why the dimensions are reversed! int[2][5] matrix2; writeln(matrix2); } ``` This prints `[[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]]` twice as they're the same matrix. I normally look at the D website, under the header `Language Reference`, which links to https://dlang.org/spec/spec.html If you're looking for just basics, the D Tour is much more friendly though, click on the `Learn` header: https://tour.dlang.org/ And then try to find what you want either in `D's Basics` or `D's Gems` (or the other headers which are specific to other topics)... these pages normally have links to more in-depth material, so it's always a good starting point. I you're looking for standard library help, then instead of clickin on `Language Reference` on the D's landing page, click on `Library Reference` instead. Almost all stdlib is either under `std` or `core`.
Jan 23
prev sibling parent Danilo <codedan aol.com> writes:
On Tuesday, 23 January 2024 at 18:15:29 UTC, Stephen Tashiro 
wrote:
 If the constructor of a class needs to create an array of 
 structs whose dimensions are inputs, what's the syntax for 
 doing this?

 For a non-example, the following program errors in main() 
 because in t.array[][] "index [0] is out of bounds".
You need to remove `auto` from `auto array = new Point[][](the_dimension,the_dimension);` because it creates a new variable.
Jan 23