1

Regarding the Structured Text Programming Language:

If I have a pointer to a table:

crcTable : ARRAY [0..255] OF WORD;
pcrcTable : POINTER TO WORD;
pcrcTable := ADR(crcTable);

and I want dereference the table at a certain index, what is the syntax to do so? I think the equivalent C code would be:

unsigned short crcTable[256];
unsigned short* pcrcTable = &crcTable[0];
dereferencedVal = pcrcTable[50]; //Grab table value at index = 50
user2913869
  • 481
  • 2
  • 5
  • 23

1 Answers1

3

You need to first move the pointer according to the array index that you want to get to. And then do the dereferencing.

// Dereference index 0 (address of array)
pcrcTable := ADR(crcTable);
crcVal1 := pcrcTable^;

// Dereference index 3 (address of array and some pointer arithmetic)
pcrcTable := ADR(crcTable) + 3 * SIZEOF(pcrcTable^);
crcVal2 := pcrcTable^;

// Dereference next index (pointer arithmetic)
pcrcTable := pcrcTable + SIZEOF(pcrcTable^);
crcVal3 := pcrcTable^;
pboedker
  • 523
  • 1
  • 3
  • 17
  • For the scope in which I am using the pointer, I don't have visibility to the original crcTable...just the pointer. So I can't use your "Dereference index 3" example. I tried doing something like: pcrcTable := pcrcTable + 3*SIZEOF(pcrcTable^); But I got a syntax error. – user2913869 Jun 20 '17 at 12:57
  • Good point about crcTable not being visible where you want to use the pointer. :-) I believe that "pcrcTable := pcrcTable + n * SIZEOF(crcVal3);" should work. The point is that you should be allowed to add anything to the pointer and SIZEOF() is only used to add the size of a word to it. – pboedker Jun 20 '17 at 18:43
  • If pcrcTable is an input variable you are probably not allowed to change it. In stead, use "pcrcTableLocal := pcrcTable + n * SIZEOF(crcVal3);" before setting crcVal3 := pcrcTableLocal^; – pboedker Jun 20 '17 at 18:54