www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Questions about multidimensional arrays - returning and initializing

reply Antti =?iso-8859-1?Q?Syk=E4ri?= <jsykari gamma.hut.fi> writes:
I've been doing a OpenGL homework app in D lately, and during this
process I've implemented some vector and matrix operations using arrays.
There are some problems, however.

1) How do I return a multidimensional 3x3 array from a function?

float[3][3] identity()
{
    const float[3][3] result = [[1,0,0],[0,1,0],[0,0,1]];

    return result;
}

Doesn't work: 
tst.d(2): functions cannot return static array float[3][3]

This is confusing, because returning just float[3] _is possible!

Consequently, I'm mostly forced to use float[][] instead of float[3][3],
with the convention that it's 3x3.  (A great part of my old code uses
float[3][3] and changing them would require plenty of work, so it might
be better to rewrite it all.)

However, there comes the next question:

2) Which is the easiest way to allocate and initialize an float[][] of
   size 3x3?

This does not work:

    float[][] mtx = new float[3][3];

("cannot implicitly convert float[3][] to float[][]")

So far I've found that this is the best way:

    float[][] mtx = new float[][3];
    result[0].length = result[1].length = result[2].length = 3;

But it is a bit clunky. Is there a better way?

(Probably writing my own array and matrix structs would be one...)

-Antti



-- 
I will not be using Plan 9 in the creation of weapons of mass destruction
to be used by nations other than the US.
Jun 13 2004
next sibling parent Antti =?iso-8859-1?Q?Syk=E4ri?= <jsykari gamma.hut.fi> writes:
In article <slrnccpr0c.jmt.jsykari pulu.hut.fi>, Antti Sykäri wrote:
 1) How do I return a multidimensional 3x3 array from a function?

 2) Which is the easiest way to allocate and initialize an float[][] of
    size 3x3?
Plus, 3) The easiest way to convert between float[][] and float[3][3]? float[3][3] my_mtx; float[][] another_mtx = make_3x3_identity_mtx(); for (int i = 0; i < 3; ++i) for (int j = 0; j < 3; ++j) my_mtx[i][j] = another_mtx[i][j]; (and vice versa...) This doesn't quite cut it. My suggestion would be just making multidimensional arrays first-class values. All in all, during this night I've just suddenly learned that arrays are very confusing. Dynamic and fixed-size arrays look the same, but actually are not quite compatible. Somehow I have the feeling that this will confuse the hell out of newbies coming from a language with just one kind of array. -Antti
Jun 13 2004
prev sibling parent Norbert Nemec <Norbert.Nemec gmx.de> writes:
Antti Sykäri wrote:

 I've been doing a OpenGL homework app in D lately, and during this
 process I've implemented some vector and matrix operations using arrays.
 There are some problems, however.
A proposal for multidimensional arrays on language level is online: http://homepages.uni-regensburg.de/~nen10015/documents/D-multidimarray.html Walter has shown general interest in it, but it will certainly need some more time until the proposal converges to some real solution. I already realized that my concepts of static and dynamic arrays were not identical to those used in D so far. (My idea was to give static arrays full value semantics. Currently, they are returned as references.) Currently, there might be workarounds for your problems. Looking for real solutions will probably prove difficult. Ciao, Nobbi
Jun 13 2004