Initializing array size

sg
2015-06-17
2015-06-24
  • sg - 2015-06-17

    The following code does not compile:

    FUNCTION foo
    VAR_INPUT CONSTANT
       x : INT;
       y : INT;
       
    END_VAR
    VAR
       dd : ARRAY [0.. x*y-1] OF LREAL;
    END_VAR
    
    dd[0] := 1;
    

    Are x and y not considered constants here?
    I get the error : Border '((x * y) - 1)' of array is no constant value

     
  • singleton - 2015-06-19

    Why do you declare the constant as INPUT? Makes no sense...

    This will work:

    FUNCTION foo
    VAR CONSTANT
       x : INT := 1;
       y : INT := 2;
       
    END_VAR
    VAR
       dd : ARRAY [0.. x*y-1] OF LREAL;
    END_VAR
    

    If you want to create the array dynamic, you have to use __NEW if your target system supports this.

     
  • TimvH

    TimvH - 2015-06-24

    There is an option to use VAR_IN_OUT CONSTANT variables.
    This is described in the Help of CODESYS

    WARNING: Uncheck the option "Replace constants", otherwise you will get exceptions in CODESYS.
    See the menu: Project => Project Settings => Compile options

     

Log in to post a comment.