加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

在Delphi/Free Pascal中:是一个运算符还是简单地表示一个指针类

发布时间:2020-12-15 10:18:24 所属栏目:大数据 来源:网络整理
导读:在Delphi / Free Pascal中:是一个运算符还是简单地表示一个指针类型? 示例代码 program Project1;{$APPTYPE CONSOLE}var P: ^Integer;begin New(P); P^ := 20; writeln(P^); // How do I read this statement aloud? P is a pointer? Dispose(P); readln;e
在Delphi / Free Pascal中:是一个运算符还是简单地表示一个指针类型?

示例代码

program Project1;

{$APPTYPE CONSOLE}

var
    P: ^Integer;

begin
    New(P);

    P^ := 20;
    writeln(P^); // How do I read this statement aloud? P is a pointer?

    Dispose(P);

    readln;
end

解决方法

当^用作类型的一部分(通常在类型或变量声明中)时,它意味着“指向”。

例:

type
  PInteger = ^Integer;

当^用作一元后缀运算符时,它意味着“取消引用该指针”。所以在这种情况下,它意味着“打印P指向”或“打印P的目标”。

例:

var
  i: integer; 
  a: integer;     
  Pi: PInteger;
begin
  i:= 100;
  Pi:= @i;  <<--- Fill pointer to i with the address of i
  a:= Pi^;  <<--- Complicated way of writing (a:= i)
            <<--- Read: Let A be what the pointer_to_i points to
  Pi^:= 200;<<--- Complicated way of writing (i:= 200)
  writeln('i = '+IntToStr(i)+' and a = '+IntToStr(a));

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读