I’ve read a piece of Delphi code like this :
sample1 = ARRAY[1..80] OF INTEGER;
psample =^sample1;
VAR
function :ARRAY[1..70] OF psample;
From my understanding, the programmer is trying to declare an array that contains 70 pointers and each pointer points to a sample1 array.
So when I write :
function[1]^[1] := 5;
function[1]^[2] := 10;
then :
function[n]^[1] := 5
function[n]^[2] := 10; ( n = 2 to 70)
Is that correct ?
Your code sample is lacking some information since you do not say how
functionis defined. This means that you cannot draw the conclusions that you attempt to draw.Of course, since
functionis a reserved word in Pascal, that code could never even compile. I will assume now that the variable is calledf.Consider the following definitions:
Here,
sample1andpsampleare types.sample1is type describing an array of 80 integers.psampleis a pointer to asample1.Next a variable named
fis defined. It is an array of 70psamples.Now, before you can even consider what happens when you write
f[1]^[1], we need to assign some values to the elements off.Suppose we did it like this:
Now it would be true that
f[i]^[k]refers to the same integer asf[j]^[k]for all validiandj. So when you writef[1]^[1] := 42you are also assigning that value tof[2]^[1],f[3]^[1]and so on.On the other hand you could do it like this:
Now each
f[i]pointer points to a distinct array in memory. In this case assigningf[1]^[1] := 42does not modify the value off[2]^[1]or any of the other values.