2017-06-19 96 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