Marco Web Center

[an error occurred while processing this directive]

Home: Code Repository: Delphi 2009 Handbook

Project: PointerMathD2007.dproj

Project Structure

PointerMathD2007.dpr
program PointerMathD2007;

uses
  Forms,
  PointerMath_MainForm_D2007 in 'PointerMath_MainForm_D2007.pas' {FormPointerMath};

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TFormPointerMath, FormPointerMath);
  Application.Run;
end.
PointerMath_MainForm_D2007.pas
unit PointerMath_MainForm_D2007;

interface

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

type
  TFormPointerMath = class(TForm)
    Memo1: TMemo;
    btnPChar: TButton;
    btnPByte: TButton;
    btnPInteger: TButton;
    Table1: TTable;
    procedure btnPCharClick(Sender: TObject);
    procedure btnPByteClick(Sender: TObject);
    procedure btnPIntegerClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormPointerMath: TFormPointerMath;

implementation

{$R *.dfm}

procedure TFormPointerMath.btnPByteClick(Sender: TObject);
var
  TenIntegers: array [1..10] of Integer;
  pOneInteger: PByte;
  I: Integer;
begin
  // write
  for I := 1 to 10 do
    TenIntegers [I] := I;
  // now read using a pointer
  pOneInteger := @TenIntegers;
  for I := 1 to 10 do
  begin
    Memo1.Lines.Add('Address: ' + IntToHex (Integer(pOneInteger), 8) +
      ' - Value: ' + IntToStr (PInteger(pOneInteger)^));
    // pOneInteger := pOneInteger + 4; // not in Delphi 2007!
    Inc (pOneInteger, 4);
  end;
end;

procedure TFormPointerMath.btnPCharClick(Sender: TObject);
var
  TenIntegers: array [1..10] of Integer;
  pOneInteger: PChar;
  I: Integer;
begin
  // write
  for I := 1 to 10 do
    TenIntegers [I] := I;
  // now read using a pointer
  pOneInteger := @TenIntegers;
  for I := 1 to 10 do
  begin
    Memo1.Lines.Add('Address: ' + IntToHex (Integer(pOneInteger), 8) +
      ' - Value: ' + IntToStr (PInteger(pOneInteger)^));
    Inc (pOneInteger, 4);
  end;
end;

procedure TFormPointerMath.btnPIntegerClick(Sender: TObject);
var
  TenIntegers: array [1..10] of Integer;
  pOneInteger: PInteger;
  I: Integer;
begin
  // write
  for I := 1 to 10 do
    TenIntegers [I] := I;
  // now read using a pointer
  pOneInteger := @TenIntegers;
  for I := 1 to 10 do
  begin
    Memo1.Lines.Add('Address: ' + IntToHex (Integer(pOneInteger), 8) +
      ' - Value: ' + IntToStr (pOneInteger^));
    // pOneInteger := POneInteger + 1;
    Inc (pOneInteger);
  end;
end;

procedure TFormPointerMath.FormCreate(Sender: TObject);
begin
  table1.Bookmark
end;

end.
PointerMath_MainForm_D2007.pas.dfm
object FormPointerMath: TFormPointerMath
  Left = 0
  Top = 0
  Caption = 'PointerMath'
  ClientHeight = 354
  ClientWidth = 628
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Memo1: TMemo
    Left = 179
    Top = 16
    Width = 433
    Height = 321
    TabOrder = 0
  end
  object btnPChar: TButton
    Left = 24
    Top = 16
    Width = 137
    Height = 25
    Caption = 'btnPChar'
    TabOrder = 1
    OnClick = btnPCharClick
  end
  object btnPByte: TButton
    Left = 24
    Top = 47
    Width = 137
    Height = 25
    Caption = 'btnPByte'
    TabOrder = 2
    OnClick = btnPByteClick
  end
  object btnPInteger: TButton
    Left = 24
    Top = 78
    Width = 137
    Height = 25
    Caption = 'btnPInteger'
    TabOrder = 3
    OnClick = btnPIntegerClick
  end
  object Table1: TTable
    Left = 120
    Top = 192
  end
end
HTML file generated by PasToWeb, a tool by Marco Cantù
Copyright 2008 Marco Cantù