Thread overview | |||||
---|---|---|---|---|---|
|
February 27, 2018 Cast a 2d static array to a 1d static array. T[s][r] -> T[s*r] | ||||
---|---|---|---|---|
| ||||
Is it possible to cast a 2d static length array to a 1d static length array? E.g. int[2][2] a = [[1,2],[3,4]]; int[4] b = cast(int[4])a; Is not the byte data in memory exactly the same? |
February 27, 2018 Re: Cast a 2d static array to a 1d static array. T[s][r] -> T[s*r] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan | On Tuesday, 27 February 2018 at 22:13:05 UTC, Jonathan wrote: > Is it possible to cast a 2d static length array to a 1d static length array? > > E.g. > int[2][2] a = [[1,2],[3,4]]; > int[4] b = cast(int[4])a; > > Is not the byte data in memory exactly the same? *( [pos,size].ptr .cst!(void*) .cst!(int[4]*) ) (using dub `cst` library) or *( cast(int[4]*)(cast(void*)([pos,size].ptr)) ) Okay, this works but is this the best way?! |
February 27, 2018 Re: Cast a 2d static array to a 1d static array. T[s][r] -> T[s*r] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan | On Tuesday, 27 February 2018 at 22:17:25 UTC, Jonathan wrote:
> On Tuesday, 27 February 2018 at 22:13:05 UTC, Jonathan wrote:
>> Is it possible to cast a 2d static length array to a 1d static length array?
>>
>> E.g.
>> int[2][2] a = [[1,2],[3,4]];
>> int[4] b = cast(int[4])a;
>>
>> Is not the byte data in memory exactly the same?
>
> *( [pos,size].ptr .cst!(void*) .cst!(int[4]*) )
> (using dub `cst` library) or
> *( cast(int[4]*)(cast(void*)([pos,size].ptr)) )
>
> Okay, this works but is this the best way?!
This should work
int[4] b = *(cast(int[4]*)a.ptr);
|
Copyright © 1999-2021 by the D Language Foundation