Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Variable size array

lucaver89
2020-04-25
2020-04-25
  • lucaver89 - 2020-04-25

    Hi to all,

    I've a little problem with the operator sizeof that is not working with in_out vars. In my case the code is very simple like below:

    FUNCTION_BLOCK fb1
    VAR_IN_OUT
        my_array    :ARRAY[*] OF STRING;
    END_VAR
    
    VAR_OUTPUT
        dimension :UINT;
    END_VAR
    
    VAR
    END_VAR
    
    
    dimension := sizeof(my_array)/sizeof(my_array[0]);
    
    PROGRAM PLC_PRG
    VAR
        array1          :ARRAY[0..100] OF STRING;
    
        pi              :fb1;
    
        dim1, dim2     :UINT;
    END_VAR
    
    pi(my_array := array1, dimension=>dim1);
    
    dim2 := SIZEOF(array1)/SIZEOF(array1[0]);
    

    In this case the result of dim1 is zero, and the result of dim2 is 101. How can I calculate the right dimension from function block?

    thanks
    Luca

     

    Last edit: lucaver89 2020-04-25
  • i-campbell

    i-campbell - 2020-04-25

    Upper_Bound(my_array,1) - Lower_Bound(my_array,1) + 1

     

Log in to post a comment.