指向同一个数组的指针数组
我读过一段像这样的Delphi代码:
sample1 = ARRAY[1..80] OF INTEGER;
psample =^sample1;
VAR
function :ARRAY[1..70] OF psample;
根据我的理解,程序员试图声明一个包含70个指针的数组,每个指针都指向一个sample1数组。
所以当我写:
function[1]^[1] := 5;
function[1]^[2] := 10;
那么:
function[n]^[1] := 5
function[n]^[2] := 10; ( n = 2 to 70)
这是正确的吗?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码示例缺少一些信息,因为您没有说明如何定义
function
。这意味着您无法得出您试图得出的结论。当然,由于
function
是 Pascal 中的保留字,因此该代码永远无法编译。我现在假设该变量名为f
。考虑以下定义:
这里,
sample1
和psample
是类型。sample1
是描述 80 个整数数组的类型。psample
是指向sample1
的指针。接下来定义一个名为
f
的变量。它是一个包含 70 个 psample 的数组。现在,在您考虑编写
f[1]^[1]
时会发生什么之前,我们需要为f
的元素分配一些值。假设我们这样做:
现在,
f[i]^[k]
确实引用与f[j]^[k]
相同的整数,对于所有有效的i
和j
。因此,当您编写f[1]^[1] := 42
时,您也会将该值分配给f[2]^[1]
,f[ 3]^[1]
等等。另一方面,您可以这样做:
现在每个
f[i]
指针都指向内存中的一个不同数组。在这种情况下,分配f[1]^[1] := 42
不会修改f[2]^[1]
的值或任何其他值。Your code sample is lacking some information since you do not say how
function
is defined. This means that you cannot draw the conclusions that you attempt to draw.Of course, since
function
is 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,
sample1
andpsample
are types.sample1
is type describing an array of 80 integers.psample
is a pointer to asample1
.Next a variable named
f
is defined. It is an array of 70psample
s.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 validi
andj
. So when you writef[1]^[1] := 42
you 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] := 42
does not modify the value off[2]^[1]
or any of the other values.这是正确的。您有 70 个指针,每个指针都指向一个包含 80 个整数的数组。
That is correct. You have 70 pointers, each pointing to an array of 80 integers.