指向同一个数组的指针数组

发布于 2025-01-01 11:39:36 字数 404 浏览 3 评论 0原文

我读过一段像这样的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静谧 2025-01-08 11:39:36

您的代码示例缺少一些信息,因为您没有说明如何定义 function 。这意味着您无法得出您试图得出的结论。

当然,由于 function 是 Pascal 中的保留字,因此该代码永远无法编译。我现在假设该变量名为 f

考虑以下定义:

type
  sample1 = array [1..80] of integer; 
  psample = ^sample1;

var
  f : array [1..70] of psample;

这里,sample1psample 是类型。 sample1 是描述 80 个整数数组的类型。 psample 是指向sample1 的指针。

接下来定义一个名为 f 的变量。它是一个包含 70 个 psample 的数组。

现在,在您考虑编写 f[1]^[1] 时会发生什么之前,我们需要为 f 的元素分配一些值。

假设我们这样做:

var
  sample: sample1;
...
for i := 1 to 70 do
  f[i] := @sample;

现在,f[i]^[k] 确实引用与 f[j]^[k] 相同的整数,对于所有有效的ij。因此,当您编写 f[1]^[1] := 42 时,您也会将该值分配给 f[2]^[1], f[ 3]^[1]等等。

另一方面,您可以这样做:

var
  samples: array [1..70] of sample1;
...
for i := 1 to 70 do
  f[i] := @samples[i];

现在每个 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 called f.

Consider the following definitions:

type
  sample1 = array [1..80] of integer; 
  psample = ^sample1;

var
  f : array [1..70] of psample;

Here, sample1 and psample are types. sample1 is type describing an array of 80 integers. psample is a pointer to a sample1.

Next a variable named f is defined. It is an array of 70 psamples.

Now, before you can even consider what happens when you write f[1]^[1], we need to assign some values to the elements of f.

Suppose we did it like this:

var
  sample: sample1;
...
for i := 1 to 70 do
  f[i] := @sample;

Now it would be true that f[i]^[k] refers to the same integer as f[j]^[k] for all valid i and j. So when you write f[1]^[1] := 42 you are also assigning that value to f[2]^[1], f[3]^[1] and so on.

On the other hand you could do it like this:

var
  samples: array [1..70] of sample1;
...
for i := 1 to 70 do
  f[i] := @samples[i];

Now each f[i] pointer points to a distinct array in memory. In this case assigning f[1]^[1] := 42 does not modify the value of f[2]^[1] or any of the other values.

宣告ˉ结束 2025-01-08 11:39:36

这是正确的。您有 70 个指针,每个指针都指向一个包含 80 个整数的数组。

That is correct. You have 70 pointers, each pointing to an array of 80 integers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文