指针类型的应用

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
type
  piny = ^integer;
var
  pany: pointer;                                  //无类型指针
  p1: pint;                                       //整型指针
  n: integer;
begin
  n := strtoint(edit1.Text);                      //n获取输入的整数
  pany := @n;                                     //pany指向n的地址
  p1 := pint(pany);                               //pany转换为整型指针并把n的地址赋值给p1
  randomize;
  p1^ := random(n);                               //随机数赋值给p1指向的n的地址
  edit2.Text := inttostr(p1^);                    //p1指向的值传给编辑框2
end;

procedure TForm1.Button2Click(Sender: TObject);   //"清除"按钮
begin
  edit1.Text := '';
  edit2.Text := '';
end;

end.

 

posted @ 2011-12-08 10:43  endsnow  阅读(182)  评论(0)    收藏  举报