TLDR; Why doesn't the code below access the passed String array correctly?
For i=1 To NumPeople TableOutputs(i+TableOffset) = !People(i) 'Compiles OK. Output Unexpected (ALL missing) Next i
I'd like to pass an array of Strings into a function and from within the function access and modify the array that was passed in. I've verified that you can pass in an array of strings, access, and modify it (such that the changes to the array will be seen from the scope of the caller).
The thing that confuses me is why I can't dereference the array and index into an array element using a Dim variable. See my comments inside the Salute() function next to the statements inside the second for-loop.
I tried indexing into the "People" array using both Dim and Public variables but only numeric literals and Constant types will index correctly. Because I needed to variably access the elements of the array (so no usage of constants), I indexed into People using a value obtained when you add a value of type "Const" and "Dim", which surprisingly worked! But the caveat is that the expression that evaluates to an index value must be enclosed in open/close parentheses.
'DEMONSTRATION: ' Using pointers to pass an array of strings into a function ' 'EXPECTED 'TableOutputs(1) = Amy TableOutputs2(1) = Amy 'TableOutputs(2) = Beth TableOutputs2(2) = Beth 'TableOutputs(3) = Claire TableOutputs2(3) = Claire 'TableOutputs(4) = Abe TableOutputs2(4) = Abe 'TableOutputs(5) = Bill TableOutputs2(5) = Bill 'TableOutputs(6) = Clark TableOutputs2(6) = Clark 'TableOutputs(7) = Amy TableOutputs2(7) = Abe 'TableOutputs(8) = Beth TableOutputs2(8) = Bill 'TableOutputs(9) = Amy TableOutputs2(9) = Clark 'TableOutputs(10)= "" TableOutputs2(10) = "" 'ACTUAL 'TableOutputs(1) = "" TableOutputs2(1) = Amy 'TableOutputs(2) = "" TableOutputs2(2) = Beth 'TableOutputs(3) = "" TableOutputs2(3) = Claire 'TableOutputs(4) = "" TableOutputs2(4) = Abe 'TableOutputs(5) = "" TableOutputs2(5) = Bill 'TableOutputs(6) = "" TableOutputs2(6) = Clark 'TableOutputs(7) = Amy TableOutputs2(7) = Abe 'TableOutputs(8) = Beth TableOutputs2(8) = Bill 'TableOutputs(9) = Amy TableOutputs2(9) = Clark 'TableOutputs(10)= "" TableOutputs2(10) = "" SequentialMode Public PTemp, Batt_volt Dim NameSet1(3) As String * 20 = {"Amy","Beth","Claire"} Dim NameSet2(3) As String * 20 = {"Abe","Bill","Clark"} Dim TableOutputs(10) As String * 20 Dim TableOutputs2(10) As String * 20 'SaluteOutput is used to view the output of the call Salute() DataTable (SaluteOutput,True,50) Sample(10,TableOutputs,String) Sample(10,TableOutputs2,String) EndTable Function Salute(People As Long, NumPeople As Long, TableOffset As Long) Const offset = 0 Dim i,j For i=1 To NumPeople TableOutputs(i+TableOffset) = !People(i) 'Compiles OK. Output Unexpected (ALL missing) Next i For j=1 To NumPeople 'TableOutputs2(j+TableOffset) = !People(offset+j) 'Error: "0 is illegal dimension index" 'TableOutputs2(j+TableOffset) = !People((offset+j)) 'Compiles OK. Output as expected 'TableOutputs2(j+TableOffset) = !People(j+offset) 'Compiles OK. Output Unexpected (ALL missing) TableOutputs2(j+TableOffset) = !People((j+offset)) 'Compiles OK. Output as expected Next j CallTable(SaluteOutput) EndFunction BeginProg Const IDX = 1 Dim DimIdx = 1 Public PubIdx = 1 Dim DimLongIdx As Long = 1 Public PubLongIdx As Long = 1 Salute(@NameSet1, 3, 0) 'Populate TableOutputs and TableOutputs2, array elements 1-3 Salute(@NameSet2, 3, 3) TableOutputs(7) = NameSet1(DimIdx) 'TableOutputs(7) = "Amy" TableOutputs(8) = NameSet1(IDX+DimIdx) 'TableOutputs(8) = "Beth" TableOutputs(9) = NameSet1(PubIdx) 'TableOutputs(9) = "Amy" TableOutputs2(7) = NameSet2(DimLongIdx) 'TableOutputs2(7) = "Abe" TableOutputs2(8) = NameSet2(IDX+PubLongIdx) 'TableOutputs2(8) = "Bill" TableOutputs2(9) = NameSet2(IDX+PubLongIdx+DimLongIdx) 'TableOutputs(9) = "Clark" CallTable SaluteOutput Scan (1,Sec,0,0) PanelTemp (PTemp,60) Battery (Batt_volt) NextScan EndProg
Thank you, Minh T., for posting this. You have uncoveredtwo bugs in CRBasic that are now fixed and will be going through testing in beta operating systems. In all your examples that did not work a work around the bugs is to put an extra set of () around the index, as in !People((i)). The first bug happens with pointers into an array when the index is a variable expression. The other issue, the erroneous compile error, will happen with any array reference when the index is an expression with the number 0 encountered first in the expression, as in X(0 + i). Again, adding the extra () around the index expression is a work around.
Again, thanks for bringing this to our attention and look for the next OS release that will fix this.