2017-06-19 92 views
0

关于结构化文本编程语言:结构化文本 - 与非关联表指针偏移

如果我有一个指针表:

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

,我想提领表在一定的指数,是什么是这样做的语法吗?我认为等效的C代码将是:

unsigned short crcTable[256]; 
unsigned short* pcrcTable = &crcTable[0]; 
dereferencedVal = pcrcTable[50]; //Grab table value at index = 50 

回答

0

您需要首先根据您想要到达的数组索引来移动指针。然后进行解引用。

// 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^; 
+0

对于我使用指针的范围,我没有可见性的原始crcTable ...只是指针。所以我不能使用你的“Dereference index 3”例子。我试着做类似这样的事情:pcrcTable:= pcrcTable + 3 * SIZEOF(pcrcTable ^);但是我有一个语法错误。 – user2913869

+0

关于crcTable在您想要使用指针的位置不可见的好处。 :-) 我相信“pcrcTable:= pcrcTable + n * SIZEOF(crcVal3);”应该管用。关键是你应该允许你将任何东西添加到指针中,而SIZEOF()仅用于为它添加一个单词的大小。 – pboedker

+0

如果pcrcTable是一个输入变量,您可能不允许更改它。相反,使用“pcrcTableLocal:= pcrcTable + n * SIZEOF(crcVal3);”在设置crcVal3之前:= pcrcTableLocal ^; – pboedker