ed:

I am trying to convert a 1D array to a 2D array

If you have a dynamic array (1D), you can convert it to a dynamic array of dynamic arrays (2D) using chunks:


void main() {
    import std.stdio, std.range, std.algorithm;

    int[] a = [1, 2, 3, 4, 5, 6];
    int[][] b = a.chunks(2).array;
    b.writeln;
}


Output:

[[1, 2], [3, 4], [5, 6]]

Your problems are caused by mixing fixed-size arrays (that are values, allocated in-place), with dynamic arrays (that are little length+pointer structs that often point to heap-allocated memory).

Bye,
bearophile

Reply via email to