[comp.windows.ms.programmer] GlobalAlloc and > 2D arrays

ddoherty@ics.uci.edu (Donald Doherty) (01/30/91)

I'll try again, taking the more direct approach...

	How does one set up a multidimensional array using GlobalAlloc?
Especially arrays 3D (i.e. array[i][j][k]) or larger.

	I've spent considerable time on this with only limited success.
Please... any hints?


Don D.
ddoherty@ICS.uci.edu

spolsky-joel@cs.yale.edu (Joel Spolsky) (02/02/91)

In article <27A5D77B.447@ics.uci.edu> ddoherty@ics.uci.edu (Donald Doherty) writes:
>	How does one set up a multidimensional array using GlobalAlloc?
>Especially arrays 3D (i.e. array[i][j][k]) or larger.

C has only limited support for multidimensional arrays, because it is
very hard for the poor compiler to figure out what their dimensions
are, especially if they have been malloc'ed. The best solution is just
to use one-dimensional arrays and figure out the offsets yourself,
i.e. in a 3 x 3 x 3 array, element a[i][j][k] is a[i*9 + j*3 +k]. You
might want to make a macro for that to clarify your code a bit, e.g., 
#define a_(x,y,z) a[(x)*9 + (j)*3 + (k)]
This is what the compiler would do anyway if it could only figure out
what shape your array was.

Hope this helped,

--
Joel Spolsky          // And these streets, Quiet as a sleeping army
spolsky@cs.yale.edu   // Send their battered dreams to heaven.   _Paul Simon