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

Increment pointer address

JorTec
2016-11-22
2016-11-30
  • JorTec - 2016-11-22

    Hello all,

    I have an array of x bytes. I want to process this data somewhere else in my program so I want to use pointers. I want to be able to increment the pointer address in an easy way. I know have the following code:


    receivedData : ARRAY [0..MAX_RECEIVE_TCP_SERVER] OF BYTE;


    processCommand( ADR(receivedData[6]) );


    FUNCTION processCommand : BOOL
    VAR_INPUT
    dataPtr : POINTER TO BYTE;
    END_VAR
    VAR
    channel: BYTE;
    value: UINT;
    state: BYTE;
    END_VAR


    channel := dataPtr^;
    dataPtr := dataPtr + 1;
    value := dataPtr^;
    dataPtr := dataPtr + 1;
    value := value + (dataPtr^ * 256);
    dataPtr := dataPtr + 1;
    state := dataPtr^;

    dummyFunction(channel, value, state);


    Is there an easier way to increment the pointer address? I would prefer to have something like this, but I don't know if this is possible:
    dummyFunction(dataPtr[0]^, dataPtr[1]^ + dataPtr[2]^ *256 , dataPtr[3]^);

     

    Related

    Talk.ru: 1
    Talk.ru: 2
    Talk.ru: 3

  • Anonymous - 2016-11-22

    Originally created by: scott_cunningham

    Instead of defining a pointer to byte, define your pointer as:

    dataPtr := POINTER TO ARRAY [0..MAX_RECEIVE_TCP_SERVER] OF BYTE;
    

    Then you can access your data with array indexing like you wish.

     
  • JorTec - 2016-11-24

    Thanks for your reply. Unfortunately, this is not possible (error 4110, '[<index>]</index>' needs array variable).

    Other solutions, or is my implementation wrong: dummyFunction(dataPtr[0]^, dataPtr[1]^ + dataPtr[2]^ *256 , dataPtr[3]^);

     

    Related

    Talk.ru: 1
    Talk.ru: 2
    Talk.ru: 3

  • Anonymous - 2016-11-26

    Originally created by: scott_cunningham

    I am not by my computer, but I believe you need to dereference before index: dataPtr^[1].

     

    Related

    Talk.ru: 1

  • JorTec - 2016-11-30

    scott_cunningham hat geschrieben:
    I am not by my computer, but I believe you need to dereference before index: dataPtr^[1].

    Works! Unbelievable I didn't try that... Thanks very much!

     

    Related

    Talk.ru: 1

  • Anonymous - 2016-11-30

    Originally created by: scott_cunningham

    If you get tired of dereferencing your pointer, you can use REFERENCE TO instead of POINTER TO. Check the help for more info.

     

Log in to post a comment.