From my second post, we can define a macro to swap 2 dynamic arrays compatible versus their dimensions (same dimension value from 1 up to 8), by only swapping the 2 array descriptors.
See the commented code below:
Code: Select all
' Only for a fbc version >= 1.08
#include once "fbc-int/array.bi"
' Swap 2 arrays of same dimensions (from 1 up to 8):
' array1, array2: array names (ending with "()") to swap (both dynamic and of same dimension)
' bool: boolean variable to receive the macro end status
' True => OK (arrays swapped)
' False => NOK (arrays not swapped because not dynamic or incompatible versus their dimensions)
#macro arraySwap(array1, array2, bool)
Scope
If (FBC.ArrayDescriptorPtr(array1)->flags And FBC.FBARRAY_FLAGS_FIXED_LEN) = 0 _
And (FBC.ArrayDescriptorPtr(array2)->flags And FBC.FBARRAY_FLAGS_FIXED_LEN) = 0 _
And FBC.ArrayDescriptorPtr(array1)->dimensions = FBC.ArrayDescriptorPtr(array1)->dimensions Then
Dim As Integer N = Sizeof(FBC.FBARRAY) - (FBC.FB_MAXDIMENSIONS - FBC.ArrayDescriptorPtr(array1)->dimensions) * Sizeof(FBC.FBARRAYDIM)
Dim As Byte Ptr p = Allocate(N)
fb_memcopy(Byval p, *FBC.ArrayDescriptorPtr(array2), N)
fb_memcopy(*FBC.ArrayDescriptorPtr(array2), *FBC.ArrayDescriptorPtr(array1), N)
fb_memcopy(*FBC.ArrayDescriptorPtr(array1), *p, N)
Deallocate(p)
bool = True '' compatible arrays
Else
bool = False '' incompatible arrays
End If
End Scope
#endmacro
Redim As String array1(1 To 9, 1 To 5) '' two-dimension var-len array, sized
For I As Integer = Lbound(array1, 1) To Ubound(array1, 1)
For J As Integer = Lbound(array1, 2) To Ubound(array1, 2)
array1(I, J) = Str(I * 10 + J)
Next J
Next I
Dim As String array2(Any, Any) '' two-dimension var-len array, unsized
Dim As Boolean b
arraySwap(array1(), array2(), b)
If b Then
Print "compatible arrays for swapping"
' printing the returned array
For I As Integer = Lbound(array2, 1) To Ubound(array2, 1)
For J As Integer = Lbound(array2, 2) To Ubound(array2, 2)
Print "'" & array2(I, J) & "'",
Next J
Print
Next I
Else
Print "Incompatible arrays for swapping"
End If
Sleep