www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - array cast from float[16] to float[4][4]

reply Spacen Jasset <spacenjasset yahoo.co.uk> writes:
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
Jan 10 2008
next sibling parent reply Christopher Wright <dhasenan gmail.com> writes:
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.
Jan 10 2008
next sibling parent Spacen Jasset <spacenjasset yahoo.co.uk> writes:
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. 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.

do the aforementioned cast. Thanks.
Jan 10 2008
prev sibling next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Christopher Wright" <dhasenan gmail.com> wrote in message 
news:fm53jh$1r5k$1 digitalmars.com...

 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.

Actually, float[4][4] is 16 consecutive floats in memory. Static arrays of static arrays are treated as rectangular arrays.
Jan 10 2008
parent Spacen Jasset <spacenjasset yahoo.co.uk> writes:
Jarrett Billingsley wrote:
 "Christopher Wright" <dhasenan gmail.com> wrote in message 
 news:fm53jh$1r5k$1 digitalmars.com...
 
 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.

Actually, float[4][4] is 16 consecutive floats in memory. Static arrays of static arrays are treated as rectangular arrays.

'proper' for what I want to do finally.
Jan 10 2008
prev sibling parent Don Clugston <dac nospam.com.au> writes:
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.
Jan 10 2008
prev sibling parent torhu <no spam.invalid> writes:
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
Jan 10 2008