Constructing arrays of structs

Renato renato at athaydes.com
Tue Jan 23 19:57:02 UTC 2024


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`.


More information about the Digitalmars-d-learn mailing list