RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
System.Addr Function

Returns a pointer to a specified object.

Pascal
function Addr(var X): Pointer;
C++
void * Addr( X);

The Addr function returns the address of a specified object. X is any variable, procedure or function identifier. The result is a pointer to X. 

The result of Addr is of the predefined type Pointer, which means that it is assignment-compatible with all pointer types but can't be dereferenced directly without a typecast.

Note: Addr is equivalent to the @ operator except that it is unaffected by the $T compiler directive.
 

Delphi Examples: 

 

{
This example uses the Addr function to get the address of a
numeric value that is stored as custom data in a tree node.
Once the assignment has been made, changes to the entries in
the NodeNumbers array can be read by dereferencing the
pointers that are stored in the Data property of the Tree
nodes.  This example requires a populated TreeView.
}

var
  NodeNumbers: array [0 .. 200] of String;

procedure TForm1.Button1Click(Sender: TObject);
var
  I : Integer;
 begin
  with TreeView1 do
  begin
    for I := 0 to Items.Count - 1 do
    begin
      NodeNumbers[I] := Items[I].Text;
      Items[I].Data := Addr(NodeNumbers[I]);
      StringGrid1.Cells[0, I + 1] := IntToStr(I);
      StringGrid1.Cells[1, I + 1] := IntToStr(Integer(Items[I].Data));
      StringGrid1.Cells[2, I + 1] := NodeNumbers[I];
    end;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  I : Integer;
  addr : Pointer;
begin
  I := StrToInt(Edit2.Text);
  addr := TreeView1.Items[I].Data;
  string(addr^) := Edit1.Text;  // NodeNumbers has been altered
  TreeView1.Items[I].Text := NodeNumbers[I]; // Update TreeView
  StringGrid1.Cells[2, I + 1] := NodeNumbers[I];  // Update StringGrid
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  StringGrid1.Cells[0, 0] := 'Node index';
  StringGrid1.Cells[1, 0] := 'Node addr';
  StringGrid1.Cells[2, 0] := 'Node text';
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!