array cast from float[16] to float[4][4]

Christopher Wright dhasenan at gmail.com
Thu Jan 10 04:45:09 PST 2008


Spacen Jasset wrote:
> If I have:
> 
> float[16]    a;
> 
> can I cast it like this cast(float[4][4])a and pass it into a function 
> expecting a float[4][4] or will this not work properly. It seems there 
> is a problem doing so.
> 
> This is for working with matricies so I should most probably write some 
> sort of class to allow row and columns access

A float[4][4] is an array of four float[4]s.
A float[4] is a (length, pointer) pair.

If multidimensional arrays are added (not just arrays of arrays), you'd 
get the syntax float[4,4], most likely.

If you want to keep the same memory, you can do:
float[16] flat;
float[4][4] square = [flat[0..4], flat[4..8], flat[8..12], flat[12..$]];

This isn't very pretty, but it grows at sqrt(N) for an original array of 
N elements, so it might be doable for your stuff.


More information about the Digitalmars-d-learn mailing list