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

torhu no at spam.invalid
Thu Jan 10 05:45:37 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.

That will work just fine, since those arrays have the same memory 
layout.  But you might want to avoid such casts, since the compiler 
won't verify that the size of the array you're casting from actually 
matches the size of what you're casting to.

Static (meaning fixed-length) arrays are implicitly converted to dynamic 
arrays when used as an argument to a function that wants a dynamic 
array.  Then you can get hold of the true length of the argument inside 
the function, and do assert(a.length == 16).  Then you'll know it's safe 
to cast it to float[4][4].  But this is getting messy, so maybe a 
different way altogether is better.  Like wrapping a one-dimensional 
array in a struct with opIndex and opIndexAssign operators.

Scroll down to Rectangular Arrays on this page:
http://www.digitalmars.com/d/arrays.html


More information about the Digitalmars-d-learn mailing list