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

Don Clugston dac at nospam.com.au
Thu Jan 10 06:59:12 PST 2008


Christopher Wright wrote:
> 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.

No it isn't. <g> It's a array of four floats, a simple block of memory.
The cast should be fine. PROVIDED that you stick to static arrays (ie, all 
dimensions specified at compile time).

> 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..$]];

You're thinking of
float [][4] square = [flat[0..4], flat[4..8], flat[8..12], flat[12..$]];

With float[4][4] there's no problem.


More information about the Digitalmars-d-learn mailing list