fxm wrote:Preparation for the next challenge (for enigma #18, using now dynamics arrays)
The similitude between raw arrays and encapsulated arrays in a Type is more difficult to code when it concerns dynamic arrays (compared to non-dynamic arrays):
- The Type structure contains only the array descriptor.
- For dynamic multi-dimensional arrays, there is no possibility of field composition with Types containing a one-dimensional array because a multi-dimensional array descriptor is not a composition of several one-dimensional array descriptors.
Therefore:
- Only one Type containing the dynamic multi-dimensional array can be used.
- The new difficulty is to get the descriptor address of a raw dynamic array.
The following example shows how to access a raw dynamic array through the structure of an encapsulated dynamic array in a Type.
The principle to get the descriptor address of the raw array is to pass the array to a procedure:
- Under the hood, its descriptor is passed by reference, so the descriptor address is passed by value as a pointer.
- For more information, see at http://www.freebasic.net/forum/viewtopi ... =3&t=23792.Code: Select all
Dim As Integer array() Redim array(1 To 5, 1 To 9) For I As Integer = 1 To 5 For J As Integer = 1 To 9 array(I, J) = I*10 + J Next J Next I Type MyArray As Integer array(Any, Any) End Type Function arrayDescriptorPtrFunction (Byval p As Any Ptr) As Any Ptr Return p End function #Define arrayDescriptorPtr(array) Cast(Function (() As Typeof((array))) As Any Ptr, @arrayDescriptorPtrFunction)(array()) '----------------------------------------- Dim Byref As MyArray a = *Cptr(MyArray Ptr, arrayDescriptorPtr(array)) '----------------------------------------- For I As Integer = 1 To 5 For J As Integer = 1 To 9 Print a.array(I, J); Next J Print Next I Print Sleep
Maybe I can propose an additional challenge, even if I'm still trying to solve it:
- Provide a UDT that wraps a multidimensional array so that success=TRUE in an example like below:
Code: Select all
dim as integer A(10,10,10) dim as UDT V => UDT( A() ) dim as boolean success => TRUE for i as integer = 0 to 10 for j as integer = 0 to 10 for k as integer = 0 to 10 if not A(i, j,k)=V[i][j][k] then success = FALSE exit for end if next k next j next i ? success