2-D array initialization

jmh530 john.michael.hall at gmail.com
Fri Jul 31 23:50:50 UTC 2020


On Friday, 31 July 2020 at 23:42:45 UTC, Andy Balba wrote:
> ubyte[3][4] c ;
>
> How does one initialize c in D ?  none of the statements below 
> works
>
>  c = cast(ubyte) [ [5, 5, 5], [15, 15,15], [25, 25,25], [35, 
> 35,35]  ];
>
> c[0] = ubyte[3] [5, 5, 5]   ;  c[1] = ubyte[3] [15, 15,15] ;
> c[2] = ubyte[3] [25, 25,25] ;  c[3] = ubyte[3] [35, 35,35] ;
>
> for (int i= 0; i<3; i++) for (int j= 0; i<4; j++) c[i][j]= 
> cast(ubyte)(10*i +j) ;

Below is for a dynamic array. You can also try mir 
(https://github.com/libmir/mir-algorithm).

import std.stdio: writeln;

void main()
{
     auto c = cast(ubyte[][]) [ [5, 5, 5], [15, 15,15], [25, 
25,25], [35, 35,35]  ];
	writeln(c);
}


More information about the Digitalmars-d-learn mailing list