To simplify the code, the arrays must first be declared fully unsized (even the number of dimensions) so that their descriptors are allocated with the maximum size (the one corresponding to their type).
From fbc version 1.08, the user can easily access any array descriptor.
The function returns by reference the descriptor of a local but static array.
This array descriptor is then used to temporarily update that of a new not yet sized array.
At the end, this new array will find its initial descriptor (before update)
See the commented code below:
Code: Select all
' Only for a fbc version >= 1.08
#include once "fbc-int/array.bi"
Function returnArray() Byref As FBC.FBARRAY
' defining a local array with static storage
Static As String array0() '' fully unsized array, so the size allocated for the array descriptor is maximum
Redim array0(1 To 9, 1 To 5)
For I As Integer = Lbound(array0, 1) To Ubound(array0, 1)
For J As Integer = Lbound(array0, 2) To Ubound(array0, 2)
array0(I, J) = Str(I * 10 + J)
Next J
Next I
' returning the array descriptor
Return *FBC.ArrayDescriptorPtr(array0())
End Function
' defining a new fully unsized array and updating its descriptor with the one returned
Dim As String array() '' fully unsized array to maximize the size allocated for the array descriptor
If (FBC.ArrayDescriptorPtr(array())->flags And FBC.FBARRAY_FLAGS_DIMENSIONS) = FBC.FB_MAXDIMENSIONS And (returnArray().flags And FBC.FBARRAY_FLAGS_DIMENSIONS) = FBC.FB_MAXDIMENSIONS Then
Swap returnArray(), *FBC.ArrayDescriptorPtr(array()) '' the two descriptors have the same size (the max size)
' printing the returned array
For I As Integer = Lbound(array, 1) To Ubound(array, 1)
For J As Integer = Lbound(array, 2) To Ubound(array, 2)
Print "'" & array(I, J) & "'",
Next J
Print
Next I
' restoring the array descriptor (before update)
Swap returnArray(), *FBC.ArrayDescriptorPtr(array())
Else
Print "Incompatible arrays"
End If
Sleep