zoukankan      html  css  js  c++  java
  • [转自万一博客园] 谈谈 Delphi 的类型与指针1

    先声明: 不要把我说的这些个东西当作教科书, 这都是自己的浅见; 同时希望得到指正.
    Delphi 的指针分为 "类型指针" 和 "无类型指针" 两类. Delphi 中的类型, 常用的也得有几百个, 我们可以给每种类型定义相应的类型指针. 其实 Delphi 已经为很多类型预定义了指针, 譬如数据类型: Integer 有对应的 PInteger; Char 有对应的 PChar; string 有对应的 PString; 再譬如: TPoint 有对应的 PPoint; TColor 有对应的 PColor 等等.
    另外, 指针也可以有指针, 譬如: PChar 是字符指针, PPChar 又是 PChar 的指针(这都是 Delphi 预定义的).
    根据上面的例子, 咱们先总结一下类型与指针的命名规则: 类型约定用 T 打头(Delphi 常规的数据类型除外, 譬如: String); 指针约定用 P 打头; 指针的指针约定用 PP 打头. 类型和指针是不可分的两个概念, 指针本身也是一种类型 - "指针类型".
    先认识一下指针相关的操作符(@、^、Addr):
    @ @变量 获取变量指针
    Addr Addr(变量)
    ^ 指针^ 获取指针指向的实际数据
    var Pxxx: ^类型 定义 Pxxx 某种类型的指针的变量 
    type Pxxx = ^类型  定义 Pxxx 为某种类型的指针
    举例说明:
    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
    
    type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        Button3: TButton;
        Button4: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
        procedure Button4Click(Sender: TObject);
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    //Integer 与 PInteger
    procedure TForm1.Button1Click(Sender: TObject);
    var
      int: Integer;
      pint: PInteger; {定义类型指针, Integer 类型的指针}
    begin
      int := 100;
      pint := @int;        {现在 pint 就是 int 的指针}
      pint^ := pint^ + 1{现在 pint^ 和 int 是一回事, 测试一下:}
      ShowMessage(IntToStr(int));   {101}
      ShowMessage(IntToStr(pint^)); {101}
    end;
    
    //直接定义类型指针
    procedure TForm1.Button2Click(Sender: TObject);
    var
      int: Integer;
      PMyInt: ^Integer;
    begin
      int := 100;
      PMyInt := Addr(int); {这句和: PMyInt := @int; 相同}
      PMyInt^ := PMyInt^ + 1;
      ShowMessage(IntToStr(int));     {101}
      ShowMessage(IntToStr(PMyInt^)); {101}
    end;
    
    //先自定义指针类型
    procedure TForm1.Button3Click(Sender: TObject);
    type
      PInt = ^Integer;
    var
      int: Integer;
      PMyInt: PInt;
    begin
      int := 100;
      PMyInt := @int;
      PMyInt^ := PMyInt^ + 1;
      ShowMessage(IntToStr(int));     {101}
      ShowMessage(IntToStr(PMyInt^)); {101}
    end;
    
    //指针的指针
    procedure TForm1.Button4Click(Sender: TObject);
    var
      int: Integer;
      pint: PInteger;
      ppint: ^PInteger;
    begin
      int := 100;
      pint := @int;
      ppint := @pint;
      ppint^^ := ppint^^ + 1;
      ShowMessage(IntToStr(int));     {101}
      ShowMessage(IntToStr(pint^));   {101}
      ShowMessage(IntToStr(ppint^^)); {101}
    end;
    
    end.

    知道以上这些就可以操作了, 就可以看懂别人的代码了; 不过要想彻底明白指针到底是怎么回事, 需要从内存谈起.
     
     
  • 相关阅读:
    01MySQL内核分析-The Skeleton of the Server Code
    debug PostgreSQL 9.6.18 using Eclipse IDE on CentOS7
    开启PG的归档模式
    11G 新特性之 密码延迟认证
    Oracle细粒度审计
    MySQL数据库回表与索引
    zabbix_api的调用(curl测试)和SQL一些解答
    【强化学习】Markov Decision processes【二】
    【强化学习】强化学习的一些基础理念【一】
    Mysql 索引优化分析
  • 原文地址:https://www.cnblogs.com/China3S/p/3254178.html
Copyright © 2011-2022 走看看