Using Typed Pointers


After you assign an address to a pointer, you can use the pointer to change the value of the variable it points to. To access the variable to which the pointer points, you have to dereference the pointer by writing the ^ symbol after the pointer name (see Listing 9-1).

Listing 9-1: Using a simple typed pointer

image from book
program Project1; {$APPTYPE CONSOLE} uses   SysUtils; var   I: Integer;   { ordinary static variable }   P: ^Integer; { a typed pointer } begin   I := 101;   WriteLn(I);   { 101 }   P := @I;        { point to I }   P^ := 202;     { change I to 202 }   WriteLn(P^); { 202 }   ReadLn; end.
image from book

To change the location to which the pointer points, you have to assign an address to the pointer. But if you want to change the value stored at the memory location to which the pointer points, you have to dereference the pointer. The WriteLn procedure knows what value to display because the P pointer is typed, and when it is derefenced, it is actually an integer value.



Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net