FB II COMPILER
Set a two dimensional array with XREF@
<< Is there any way to use XREF with a two dimensional array? One of the dimensions will be fixed, but the other will not be known until runtime.
DIM myArray(whoKnows, 31)
XREF@ myArray(?) <--- What goes here? >>
You can have as many dimensions as you want in an XREF array. When declaring it, you can put an arbitrary positive constant as the first dimension; the first dimension is basically ignored by the compiler. The second & subsequent dimensions have to be correctly known in advance. So you can declare it like this:
XREF@ myArray(1,31)
Even though the first dimension is set to "1" here, it's still safe to use subscripts greater than 1 when you actually reference the elements of the
array.
The only caveat is if you turn on the "Check array bounds" preference: in that case, you should declare that first dimension to some large constant which you know your subscript values will never exceed--otherwise you could get an "array out of bounds" error during run time.
I would like to add to Rick's response some notes I keep around to help me with my math when it comes to arrays and using XREF:
All of the following will create a block of memory that is 1024-bytes long:
DIM myVar%(511)
| |___ 512 rows (incl 0th element)
|_____ 2 bytes width per location
DIM myVar%(255,1)
| | |___ 2 columns (0,1)
| |_______ 256 rows (incl 0th element)
|_________ 2 bytes width per row/column cell
DIM myVar&(255)
| |___ 256 rows (incl 0th element)
|_____ 4 bytes width per location
DIM myVar&(127,1)
| | |___ 2 columns (0,1)
| |_______ 128 rows (incl 0th element)
|_________ 4 bytes width per row/column cell
I also have the layout for string arrays, but, you get the picture. I agree with Rick "You can have as many dimensions as you want in an XREF array", but, I want to add that the array size you choose in XREF should agree with the size of the original array memory block... eg
LOCAL FN useIt(arrayPtr&)
XREF myNewVal%(511)
myNewVal& = arrayPtr&
myNewVal%(3) = 4 ' replaces value 6 in orig array
END FN
CLEAR LOCAL
DIM myVal%(511)
LOCAL FN setItUp
myVal%(3) = 6
FN useIt(@myVal%(0))
END FN
The key that helped me keep track of the XREF thingie - and Rick's comment is right on, is that I know how the data is arranged inside the array (memory block). That way, I can manipulate the original data anyway I want with various XREF statements and still retain the integrity of the original array.
I appologize if I went overboard with this, but, I too scratched my head with this XREF thing, BTW, it is an excellant instruction and I use it whenever I get the chance.
|