CODESYS - the IEC 61131-3 automation software
https://forum.codesys.com/

Increment pointer address
https://forum.codesys.com/viewtopic.php?f=2&t=7372
Page 1 of 1

Author:  JorTec [ Tue Nov 22, 2016 10:21 am ]
Post subject:  Increment pointer address

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]^);

Author:  scott_cunningham [ Tue Nov 22, 2016 12:44 pm ]
Post subject:  Re: Increment pointer address

Instead of defining a pointer to byte, define your pointer as:
Code:
dataPtr := POINTER TO ARRAY [0..MAX_RECEIVE_TCP_SERVER] OF BYTE;


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

Author:  JorTec [ Thu Nov 24, 2016 8:08 am ]
Post subject:  Re: Increment pointer address

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

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

Author:  scott_cunningham [ Sat Nov 26, 2016 8:06 pm ]
Post subject:  Re: Increment pointer address

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

Author:  JorTec [ Wed Nov 30, 2016 3:40 pm ]
Post subject:  Re: Increment pointer address

scott_cunningham wrote:
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!

Author:  scott_cunningham [ Wed Nov 30, 2016 7:45 pm ]
Post subject:  Re: Increment pointer address

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

Page 1 of 1 All times are UTC+01:00
Powered by phpBB® Forum Software © phpBB Limited
https://www.phpbb.com/