How do I pass a array throught a function and return an array - Pointers

Volvo742
2018-04-16
2018-04-16
  • Volvo742 - 2018-04-16

    Hi!

    I want to pass a array of strings throught a function and that function is going to return back the array. So I need to use pointers for that.

    Let's keep it simple!
    I first have my array:

    myStrArray: ARRAY[0..10] OF STRING;
    

    Then I need to convert myStrArray to a pointer.

    p_myStrArray: POINTER := ADR(myStrArray);
    

    Then I need to pass p_myStrArray throught my function

    p_returnArray := myFunc(p_myStrArray);
    (*....*)
    FUNCTION myFunc: POINTER
    VAR_INPUT
    Β  Β p_myStrArray: POINTER;
    END_VAR
    VAR
    END_VAR
    myFunc := p_myStrArray; // Return statement
    

    And then recive my pointer and turn it back to an array

    returnArray: ARRAY[0..10] OF STRING;
    returnArray := p_returnArray^;
    

    But it won't work for me. What have I missed?

     
  • josepmariarams - 2018-04-16

    Wrap your array with an fb and pass the fb to the funtion as varinout. Or better, the funtion could be an method of the fb.

    If not, you don t know from inside the funtion array dimension.

     
  • Anonymous - 2018-04-16

    Originally created by: rickj

    I didn't know the compiler would allow this

    FUNCTION myFunc: POINTER
    VAR_INPUT
    Β  Β p_myStrArray: POINTER;
    END_VAR
    

    Maybe try this instead

    FUNCTION myFunc: POINTERΒ  ARRAY[0..10] OF STRING
    VAR_INPUT
    Β  Β p_myStrArray: POINTERΒ  ARRAY[0..10] OF STRING;
    END_VAR[/quote]
    

    Then when you call the function you may try this

    VAR
    Β  Β  MyStrArray: ARRAY[0..10] OF STRING;
    Β  Β  PtrReturnArray : POINTER TO ARRAY[0..10] OF STRING;
    END_VAR
    PtrReturnArray := myFunc(ADR(MyStrArray));
    
     

Log in to post a comment.