bbanelli wrote:is there a way to initialize array as in C?
[...]
is there some elegant way such as:
Code: Select all
Fa() = {361, 575, 922, 525, 176, 586, 640, 321, 536, 742, 677, 742, 687, 284, 193, 517, 273, 494, 263, 147, 593, 800, 571, 320, 803, 133, 231, 390, 685, 330, 63, 410}
Code: Select all
;=================================
;
; works with types a,b,c,w,u,l,i,q,f,d,s
;
Macro SetTypeSizes(_type_,_name_) : #__#_type_#__size = SizeOf(_name_) : EndMacro
SetTypeSizes(a,Ascii) : SetTypeSizes(b,Byte) : SetTypeSizes(w,Word) : SetTypeSizes(u,Unicode)
SetTypeSizes(l,Long) : SetTypeSizes(i,Integer) : SetTypeSizes(q,Quad) : SetTypeSizes(f,Float)
SetTypeSizes(d,Double): SetTypeSizes(s,String) : SetTypeSizes(c,Character)
Macro CreateArray(_name_,_type_)
Dim _name_._type_( (?__CreateArray__End__Label_#MacroExpandedCount - ?__CreateArray__Start__Label_#MacroExpandedCount) / #__#_type_#__size )
Define __CreateArray__loop__#MacroExpandedCount
Restore __CreateArray__Start__Label_#MacroExpandedCount
For __CreateArray__loop__#MacroExpandedCount = 0 To ArraySize(_name_()) - 1
Read._type_ _name_( __CreateArray__loop__#MacroExpandedCount )
Next
DataSection : __CreateArray__Start__Label_#MacroExpandedCount: : Data._type_
EndMacro
Macro EndCreateArray(_name_)
: __CreateArray__End__Label_#MacroExpandedCount: : EndDataSection
EndMacro
;---------------------------------
CreateArray(Fa,i) 361, 575, 922, 525, 176, 586, 640, 321, 536, 742, 677,
742, 687, 284, 193, 517, 273, 494, 263, 147, 593, 800,
571, 320, 803, 133, 231, 390, 685, 330, 63, 410
EndCreateArray(Fa)
Debug ArraySize( Fa() )
Debug "-----"
For i = 0 To ArraySize( Fa() ) - 1
Debug Fa(i)
Next
Debug "================================="
CreateArray(theStrings,s) "abc", "def", "ghi", "jkl" EndCreateArray(theStrings)
For i = 0 To ArraySize( theStrings() ) - 1
Debug theStrings(i)
Next
Code: Select all
EnableExplicit
Macro Arr(_type_)
?__Arr__Label_#MacroExpandedCount
DataSection : __Arr__Label_#MacroExpandedCount: : Data._type_
EndMacro
Macro EndArr()
: EndDataSection
EndMacro
;---------------------------------
Macro DefMemArray(_type_,_name_) : Structure _name_#Array : _type_._type_[0] : EndStructure : EndMacro
DefMemArray(a,Ascii) : DefMemArray(b,Byte) : DefMemArray(w,Word) : DefMemArray(u,Unicode)
DefMemArray(l,Long) : DefMemArray(i,Integer) : DefMemArray(q,Quad) : DefMemArray(f,Float)
DefMemArray(d,Double): DefMemArray(c,Character) : DefMemArray(s,String)
Define i, *p.IntegerArray
*p = Arr(i) 361, 575, 922, 525, 176, 586, 640, 321, 536, 742, 677,
742, 687, 284, 193, 517, 273, 494, 263, 147, 593, 800,
571, 320, 803, 133, 231, 390, 685, 330, 63, 410
EndArr()
For i = 0 To 31
Debug *p\i[i]
Next
Debug "================================="
Define *b.ByteArray = Arr(b) 1,2,3,4,5,6,7,8,9,10 EndArr()
For i = 0 To 9
Debug *b\b[i]
Next
Debug "================================="
; String pointers
Define *s.StringArray = Arr(i) @"abc", @"def", @"ghi", @"jkl" EndArr()
For i = 0 To 3
Debug *s\s[i]
Next
Code: Select all
;=================================
;
; Using CopyMemory, does not work with strings
;
Macro CreateArray(_name_,_type_)
Dim _name_._type_(1)
ReDim _name_._type_( (?__CreateArray__End__Label_#MacroExpandedCount - ?__CreateArray__Start__Label_#MacroExpandedCount) / (@_name_(1)-@_name_(0)) )
Debug "CreateArray"
CopyMemory(?__CreateArray__Start__Label_#MacroExpandedCount, @_name_(0), (ArraySize(_name_()))*(@_name_(1)-@_name_(0)))
DataSection : __CreateArray__Start__Label_#MacroExpandedCount: : Data._type_
EndMacro
Macro EndCreateArray(_name_)
: __CreateArray__End__Label_#MacroExpandedCount: : EndDataSection
EndMacro
;---------------------------------
CreateArray(Fa,i) 361, 575, 922, 525, 176, 586, 640, 321, 536, 742, 677,
742, 687, 284, 193, 517, 273, 494, 263, 147, 593, 800,
571, 320, 803, 133, 231, 390, 685, 330, 63, 410
EndCreateArray(Fa)
Debug ArraySize( Fa() )
Debug "-----"
For i = 0 To ArraySize( Fa() ) - 1
Debug Fa(i)
Next