Cannot pass array of constant size to a function as a reference

apurv
2024-01-07
2024-01-09
  • apurv - 2024-01-07

    I have a Array of constant size defined like this

    Var Constant
    MAX_SIZE :UINT := 10;
    End_var
    
    VAR
    array : ARRAY[0..MAX_SIZE] OF INT;
    END_VAR
    

    Now I want to pass this array to a function by reference,

    Function fun : INT
    VAR_IN_OUT CONSTANT
    MAX_SIZE : UINT;
    END_VAR
    VAR_INPUT
    array : REFERENCE TO ARRAY[0..MAX_SIZE] OF INT;
    END_VAR
    

    but when I run this it gives strange Errors

    Error : Cannot Convert type 'ARRAY [0..MAX_SIZE] OF INT' to type 'ARRAY[0..MAX_SIZE] OF INT'
    
     

    Last edit: apurv 2024-01-07
  • apurv - 2024-01-08

    Could not find the solution to this issue but I have side stepped this by creating a global variable MAX_SIZE.

     
  • tvm - 2024-01-08

    maybe this would be a better approach, then you don't have to pass the constant at all.

    FUNCTION fun : INT
    VAR_IN_OUT
        arr:        ARRAY[*] OF INT;
    END_VAR
    VAR
        lower:      DINT;
        upper:      DINT;
    END_VAR
    
    lower:= LOWER_BOUND(arr, 1);
    upper:= UPPER_BOUND(arr, 1);
    

    see here as well
    https://help.codesys.com/api-content/2/codesys/3.5.12.0/en/_cds_datatype_array/

     

    Last edit: tvm 2024-01-08
    • apurv - 2024-01-09

      okay but will this pass the array by copying it or it will be a reference to original array only.

       
  • tvm - 2024-01-09

    It will be a reference, because it's a VAR_IN_OUT.

    it's a little weird debugging the array online. It just shows as a POINTER TO INT, and you can't actually see the array from the function side. But you can still work with it as a normal array, not a pointer.

     

Log in to post a comment.