Structure Type (2)

Source: Internet
Author: User

Structure pointer

Pointer

A pointer is a variable that stores the memory address of a specified type (or undefined type) variable. Therefore, a pointer indirectly references a value. Define a pointer using a special character instead of a specific keyword. This special character is a delimiters (^), as shown in the following example:

type  PointerToInt = ^Integer;

Once you define a pointer variable, you can use the @ symbol to assign the address of another variable of the same type to it. See the following example:

var  P: ^Integer;  X: Integer;begin  P := @X;  // change the value in two different ways  X := 10;  P^ := 20;  

If a pointer P is defined, P indicates the memory address pointed to by the pointer, and P ^ indicates the actual content stored in the memory. Therefore, in the above Code, P ^ is equal to X.

In addition to the address indicating the allocated memory, the pointer can also passNewThe routine dynamically allocates memory in the heap. However, when you do not need this pointer, you must also callDisposeThe routine releases your dynamically allocated memory.

var  P: ^Integer;begin  // initialization  New (P);  // operations  P^ := 20;  ShowMessage (IntToStr (P^));  // termination  Dispose (P);end;

If the pointer has no value, you can assign it nil. In this way, you can check whether the pointer is nil to determine whether the pointer currently references a value. This is often used because accessing a null pointer value will cause an access conflict error, that is, the "general protection error" (GPF) that everyone knows ). See the following example:

procedure TFormGPF.BtnGpfClick(Sender: TObject);var  P: ^Integer;begin  P := nil;  ShowMessage (IntToStr (P^));end;-------------------------------------------------------------------------------------------------
In case of a blog

Type
TRec = record {define the structure TRec}
Name: string [12];
Age: Word;
End;
TPRec = ^ TRec; {define the pointer type of the TRec structure TPRec}

Var Rec: TRec; {declare Structure Variable}
PRec1, PRec2: TPRec; {declare the TPRec pointer variable}
Prec3: ^ TREC; {pointer variable that declares the TREC structure. Currently, prec1 and prec3 are different types of variables}
P: pointer; {declare No type pointer}


// Access the structure through the structure pointer
Procedure tform1.button1click (Sender: tobject );
Begin
Rec. Name: = 'zhang san ';
Rec. Age: = 18;

Prec1: = @ REC; {tell the REC address to prec1}

{Should have accessed this way}
Showmessage (prec1 ^. Name); {Zhang San}

{Delphi allows simple use of structure pointers}
Showmessage (prec1.name); {Zhang San}

{If we modify the data through a pointer}
Prec1.name: = 'Li si ';

{So}
Showmessage (Rec. Name); {Li Si}
{Because prec1 and REC refer to the same data}
End;


// If the pointer is used separately, the memory must be given first.
Procedure TForm1.Button2Click (Sender: TObject );
Begin
GetMem (PRec2, SizeOf (TRec ));
PRec2.name: = 'wang wu ';
PRec2.age: = 9;

ShowMessage (PRec2.name); {Wang Wu}

{Manually assigned memory, which must be manually released}
FreeMem (PRec2 );
End;


// Although it is a pointer to the same structure, it is not a type
Procedure TForm1.Button3Click (Sender: TObject );
Begin
Rec. name: = 'Sun 6 ';
Rec. age: = 16;

{Give the Rec address to the PRec3 pointer of the ^ Rec type}
PRec3: = @ Rec;
ShowMessage (PRec3.name); {sun 6}

{If You Want To Tell PRec1 the value that PRec3 knows, type conversion is required} PRec1: = TPRec (PRec3); ShowMessage (PRec1.name); {sun 6}

{What if I want to tell PRec3 the value that PRec1 knows ?}
Rec. name: = 'zhao 7 ';
Rec. age: = 24;
PRec1: = @ Rec;
ShowMessage (PRec1.name); {Zhao Qi}

{Convert like this}
TPRec (PRec3): = PRec1;
ShowMessage (PRec3.name); {Zhao Qi}
End;


// Read and write data in TRec with a non-type pointer
Procedure TForm1.Button4Click (Sender: TObject );
Begin
Rec. name: = 'duba ';
Rec. age: = 36;

P: = @ Rec;

{Assignment}
TPRec (P). name: = 'hou jiu ';

{Value}
ShowMessage (TPRec (P). name); {Hou JIU}
End;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.