I have seen many posts relating to indirectly addressing arrays but none seem to fully explain how to do it simply. At the moment I have an application where I would like to dynamically set the size of an array then access the individual elements using an 'index'. So far I have tried the following in SCL but it does not compile:-
VAR_Input
nElements:int; // number of elements in array
End_Var
VAR_Temp
myArray:array[1..nElements] of dint; //my dynamic array
myElement:int; //my pointer to a specific element
myValue:dint; // a temporary place holder for this example
End_Var
The declaration for the array fails because 'nElements' is an invalid constant, if I declare a constant it works but my array is no longer dynamic. Does anyone know why I can't use the input as the constant?
The next problem is stepping through the array. I would like to use a loop with an expression like: -
myElement:=myElement+1;
myValue:=myArray[myElement];
Again this does not compile saying I have an invalid constant. I could use an old piece of code I got from Siemens Tech. support years ago to get a solution but it would not be as flexible or elegant as being able use indirect addressing and dynamic array sizing.
Has anyone got any suggestions?
VAR_Input
nElements:int; // number of elements in array
End_Var
VAR_Temp
myArray:array[1..nElements] of dint; //my dynamic array
myElement:int; //my pointer to a specific element
myValue:dint; // a temporary place holder for this example
End_Var
The declaration for the array fails because 'nElements' is an invalid constant, if I declare a constant it works but my array is no longer dynamic. Does anyone know why I can't use the input as the constant?
The next problem is stepping through the array. I would like to use a loop with an expression like: -
myElement:=myElement+1;
myValue:=myArray[myElement];
Again this does not compile saying I have an invalid constant. I could use an old piece of code I got from Siemens Tech. support years ago to get a solution but it would not be as flexible or elegant as being able use indirect addressing and dynamic array sizing.
Has anyone got any suggestions?