Here is Steve's program converted to FreeBasic.
/' title: arraytst.c
by: sarbayo, (c) 2001
Desc: Dynamic Multi-Dimensional arrays.
This example illustrates how to dynamically create and access,
store to and retrieve from, multi-dimensional arrays,
such as these Basic statements:
<snip>
DIM an_array(3,3,3)
an_array(a,b,c) = value
value = an_array(a,b,c)
PRINT value
<snip>
See comments.
'/
#define MAX_VARS 100
#define VAR_NAME 33
#Define NULL 0
Type array_integer
iv_array As Integer Ptr
in_array As String
End Type
Dim Shared iarray As array_integer
Sub IntArray() '/* --- integer array --- */
'/* Basic statement: DIM an_array(3,3,3) */
Dim As integer L = 3, W = 3, D = 3 '/* Length, Width, Depth. */
'/* Note: L,W,D have to be stored somewhere for as long */
'/* as this array is in use. Perhaps within the structure. */
'/* array's linear size: 3x3x3 = 27 */
Dim As Integer size = L * W * D
Dim As Integer ii, x
Dim As Integer a = L, b = W, c = D
iarray.in_array = "integer" '/* give this array a name */
'/* now allocate memory for the array to hold 27 integers,*/
'/* where: iarray. = structure, iv_array = integer array */
iarray.iv_array = Callocate(size, sizeof(Integer))
If iarray.iv_array = NULL Then
Print "Could not allocate array."
Exit Sub
EndIf
'/* now fill the array with some data, (for simplicity), */
'/* using the "linear" fashion: array[0]...[26] */
For ii = 0 To size - 1
iarray.iv_array[ii] = (c*ii)
Next
'/* Display array contents: */
For ii = 0 To size - 1
Print iarray.in_array & "(" & ii &") = " & iarray.iv_array[ii]
Next
'/* Basic statement: x = an_array(1,1,1)
' or (a,b,c),
'/* here a 'C' routine would interpret the above Basic */
'/* statement and make the assignments to a,b,c. */
a = 1 '/* 0 to 2 */
b = 1 '/* feel free to change the values of: */
c = 1 '/* a,b,c to see the results. */
'/* the algorithm to calculate 3-Dimensional array offset */
x = iarray.iv_array[c+D*(b+W*a)] '/* that's it !!! */
Print
Print "Index a = " ; a
Print "Index b = " ; b
Print "Index c = " ; c
Print "Value = " ; x
End Sub
'/*--------- end integers ------------*/
IntArray
Sleep
If iarray.iv_array <> NULL Then
DeAllocate iarray.iv_array
End If