user2913869
user2913869

Reputation: 521

Structured Text - dereference table pointer with offset

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

Upvotes: 1

Views: 4011

Answers (1)

pboedker
pboedker

Reputation: 533

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

Upvotes: 3

Related Questions