What do the operators @ and ^ ?? Just want to know what will happen in Pkts if Psh [4, 1] =?

Blocks = 4; RTParts = 2; BlNum, ROTO: integer mkts = array [0..3, 1..6, 1..16] of SmallInt; msh = array[1..392] of SmallInt; TPmkts = ^mkts; TPmsh = ^msh; Pkts: array[1..Blocks, 1..RTParts] of TPmkts = ( (nil, nil), (nil, nil), (nil, nil), (nil, nil) ); Psh: array[1..Blocks, 1..RTParts] of TPmsh = ( (nil, nil), (nil, nil), (nil, nil), (nil, nil) ); Pkts[Num, ROTO]:= @Psh[Num, ROTO]^[9]; 
  • Add a Pkts and Psh variable Pkts Psh . Apparently they are multidimensional arrays - Kromster

1 answer 1

The @ operator means taking the address of a variable (getting a pointer to a variable)

The ^ operator in the code denotes dereferencing a pointer and getting the value of a variable.

The ^ operator when declaring a type means using a pointer. For example, PPoint = ^TPoint means that an object of type PPoint will be a pointer to an object of type TPoint .

These operators complement each other.

 var a: integer; b: pointer; begin a := 123; // теперь b указывает на a b := @a; // приводим указатель к конкретному типу и записываем в адрес, // на который указывает b - новое значение 120 // теперь a будет 120 PInteger(b)^ := 120; end; 

What will happen in Pkts if Psh [4, 1] = 230?

 Pkts[4, 1]:= @Psh[4, 1]^[9]; 

In Pkts[4,1] will be an element with offset 9 from Psh[4,1] .

Let's take a look at the steps, we get the address of the Psh variable [4,1], right there we deline it back into a variable, and from this variable we take an element (that is, it looks like a string or another array).

Please note that this code will not compile in Delphi, since The delimited type @Psh[4, 1] not specified.

  • one
    This is debugging code. I think it will be easier for you to independently test what happens there. - Kromster
  • one
    @creamsun forward naturally. - Kromster
  • 2
    @creamsun pointer can not be equal to 230, this is too small address - Pavel Mayorov
  • one
    @creamsun most likely, because types will not match. To refer to element 9, it must exist. To refer to the 1st element of the array from the 3rd element of the array from the 0th element of the array, they all must exist and be accessible. - Kromster
  • 2
    @creamsun is a slightly different concept. The offset occurs in a one-dimensional array - in memory. Appeal to the element of the array (s) occurs according to the structure of the element in the arrays. How specifically the structure of the arrangement of arrays in memory is implemented, I will not remember now. Need to check. Perhaps you should prepare ru.stackoverflow.com/help/mcve and ask a specific question about your problem. - Kromster