How do I pass multidimensional static arrays to functions expecting dynamic arrays?

Timon Gehr timon.gehr at gmx.ch
Mon Aug 29 17:10:00 PDT 2011


On 08/30/2011 01:29 AM, Andrej Mitrovic wrote:
> Take a look:
>
> void main()
> {
>      int[2] single;
>      // foo(single);  // no
>      foo(single[]);   // int[2][] slice, ok
>
>      int[2][2] multi;
>      // bar(multi);     // int[2][2] no
>      // bar(multi[]);   // int[2][] slice, no
>      // bar(multi[][]); // int[2][] slice, no
> }
>
> void foo(int[] value) {}
> void bar(int[][] value) {}
>
> I can easily slice a one-dimensional static array, but I can only
> slice a single dimension. So how do I pass a multidimensional static
> array to a function expecting a multidimensional dynamic array?

D does not have multidimensional dynamic arrays.

To solve your problem, you have to manually create an array of 1D-slices:

Like this:

bar(array(map!((int[] a){return a;})(multi[])));

Or like this:

int[][] arg=new int[][](2);
foreach(i,ref x;arg) x=multi[i][];
bar(arg);







More information about the Digitalmars-d-learn mailing list