Pass the second array dimension into a function as a parameter

joanm
2022-05-04
2022-05-10
  • joanm - 2022-05-04

    Hello all,

    I am working on a program in which I have declared one two dimensional array like this:

    arrRawData : ARRAY [1..6,1..38] OF WORD;
    

    This array holds 38 words of raw data for 6 machine parts.

    I need to pass the second dimension array to a function:

    FUNCTION myDataReceiverFunction : BOOL
    VAR_INPUT
      arrRawDataWords : ARRAY[1..38] OF WORD;
    END_VAR
    ... 
    

    But, when I try to call the function like this:

    myDataReceiverFunction(arrRawData[1]);
    

    The compiler alerts me that: Cannot convert type 'WORD' to type 'ARRAY[1..38]OF WORD'.

    I can use pointers, but, is there any easier syntax that would allow me to do this?

    Thank you in advance!

     

    Related

    Talk.ru: 1

  • joanm - 2022-05-04

    Answering myself just in case other people gets stuck here:

    I should have declared the array like this:

    arrRawData : ARRAY [1..6] OF ARRAY[1..38] OF WORD;
    

    Then my syntax does not make the compiler to go nuts.

    So it is solved.

     
  • ojz0r - 2022-05-05

    An explaination what is happening:
    When you declare the array it is basically a custom variable type and the input to the function needs to match exactly that even though the second dimension would look the same as a one dimensional array uncompiled.

     
  • tvm - 2022-05-05

    What's actually the difference between ARRAY [1..6,1..38] OF WORD, and ARRAY [1..6] OF ARRAY[1..38] OF WORD? It seems to me that they're the same thing. In either case arrRawData[1] should be an ARRAY [1..38] OF WORD. Other programming languages treat arrays in this way, is this just one of those Codesys quirks?

     
    • maretxa - 2022-05-05

      Hi,

      Not sure but :
      If you use ARRAY [1..6,1..38] OF WORD you can't use a variable to reach your table yourtable[1,1]

      But with ARRAY [1..6] OF ARRAY[1..38] OF WORD you can easly use a boucle FOR to search what you want in your table yourtable[1][1] OR yourtable[iIndex1][iIndex2]

       

      Related

      Talk.ru: 1

      • tvm - 2022-05-05

        Maybe I misunderstand what you're saying, but I regularly index 2 dimensional arrays with variables, for example:

        myArray: ARRAY[1..10,1..10] OF INT;
        
        FOR i:= 1 TO 10 DO
            FOR j:= 1 TO 10 DO
                myArray[i,j]:= i;
            END_FOR
        END_FOR
        
         
  • joanm - 2022-05-08

    @tvm it seems that you can't pass myArray[3] (from your example code) into a function that expects to get an array of 10 INTs.

    Not a big deal, but seems strange.

     

    Related

    Talk.ru: 3

  • ludecus

    ludecus - 2022-05-09

    While you need to work with array, there are two options:

    Declare a dynamic array size as input of the function (maybe it could require a FB instead)

    VAR_IN_OUT
        arrData : ARRY * OF WORD;
    END_VAR
    

    During the compile the size of the dynamic array will be scaled.

    Or you could use a pointer

    VAR_INPUT
        parrData : POINTER TO WORD;
        nDataSize : UINT;
    END_VAR
    

    Within the function or FB the pointer should be handled.

    At least both ways are more generic, if the array size has been changed.

     
  • joanm - 2022-05-10

    Thanks for your comment @ludecus, in any case, I'm very familiar with pointers and use them extensively in my daily basis, but it's not the solution I'm after now.

    Thanks again.

     

Log in to post a comment.