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

Advanced pointer to struct members

2018-12-06
2022-01-27
  • tjarcoboerkoel - 2018-12-06

    Hi readers,

    If I have a structure called sHisto:
    STRUCT
    History: ARRAY [1..64] OF REAL:
    MaxValue: REAL;
    MinValue: REAL;
    AvgValue: REAL;
    END_STRUCT

    And I pass it to a functionblock as a pointer:
    Measurement: POINTER TO sHisto;

    How do I reference the members inside a functionblock? Is the following right?
    Measurement.History[3] := 1.0; //set a single array value
    temp := Measurement.History[3]^ ; //get a single array value
    Measurement.MaxValue := 5.0; //set a member variable
    temp := Measurement.MaxValue^; //get a member variable

    SomePointerToReal := ADR(Measurement.MaxValue); //pass the addr of a value
    SomePointerToRealArr := ADR(Measurement.History); //pass the address of an array
    SomePointerTosHisto := (Measurement); //Pass the address of the structure

    Best regards,

     
  • Lo5tNet - 2018-12-06

    Someone will correct me if i'm wrong but when you use a pointer and want to access the values directly you need to de-reference it by using the ^. The de-reference comes after the variable pointer name and before the struct member.

    So to access a pointer inside the FB you would use:
    Measurement^.History[3] := 1.0; //set a single array value
    temp := Measurement^.History[3] ; //get a single array value
    Measurement^.MaxValue := 5.0; //set a member variable
    temp := Measurement^.MaxValue; //get a member variable

    If you are trying to pass the addr of a value while still in the FB you wouldn't need the ADR() since it is already referenced as a pointer. Otherwise you are passing an address of an address.
    SomePointerToReal := Measurement.MaxValue; //pass the addr of a value
    SomePointerToRealArr := Measurement.History; //pass the address of an array
    SomePointerTosHisto := Measurement; //Pass the address of the structure

     

    Related

    Talk.ru: 3

  • alebrije - 2022-01-27

    Hello,
    I would like to save some values in a structure named Test
    The structures is

    TYPE test :
    STRUCT
    generalinfo :general; // 2 variables as string
    testfreatures : freatures; // 2 variables as string
    END_STRUCT
    END_TYPE

    I'm using a pointer to the struct test
    VAR
    INIValues1 : POINTER TO ARRAY [0..1000] OF STRING;
    END_VAR
    INIValues1 := ADR(Test);
    // I want to write the values as follow
    INIvalues1^[0].^[0] := 'Test';

    How can I mange to do this... I would like later only to replace the struct so my function fits for many different structures

     
  • fajean - 2022-01-27

    To achieve what you want, you do not declare a "POINTER TO ARRAY[] OF STRING". You declare a "POINTER TO STRING". You can use the array subscript notation directly with this pointer. The array subscript notation performs dereferencing, just like using "^". In fact, ptr[0] and ptr^ are equivalent as far as I can tell.

    PROGRAM MAIN
    VAR
      INIValues1 : ARRAY [0..1000] OF STRING;
      INIValues1_ptr : POINTER TO STRING;
    END_VAR
    
    INIValues1_ptr := ADR(INIValues1);
    INIValues1_ptr[0] := 'Test';
    
     

Log in to post a comment.