Learning structure [Record] type (4)-structure pointer

Source: Internet
Author: User
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) button1: tbutton; button2: tbutton; button3: tbutton; button4: tbutton; Procedure submit (Sender: tobject ); end; var form1: tform 1; implementation {$ R *. DFM} type TREC = record {definition structure TREC} Name: String [12]; age: word; end; tprec = ^ TREC; {pointer type tprec} var REC: TREC; {declared Structure Variable} prec1, prec2: tprec; {declared tprec pointer variable} prec3: ^ TREC; {declare the pointer variable of the TREC structure. Now prec1 and prec3 are different types of variables} p: pointer; {declare No type pointer} // access the structure procedure tform1.button1click (Sender: tobject); begin rec. name: = 'zhang san'; Rec. age: = 18; prec1: = @ REC; {tell prec1 the REC address.} {Should have accessed} showmessage (prec1 ^. name); {zhangsan} {Delphi allows the simple use of the structure pointer} showmessage (prec1.name); {zhangsan} {if we modified the data through the pointer} prec1.name: = 'lily '; {so} showmessage (Rec. name); {Li Si} {because prec1 and REC refer to the same data} end; // if you use a pointer separately, you must first give the memory procedure tform1.button2click (Sender: tobject ); begin getmem (prec2, sizeof (TREC); prec2.name: = '王'; prec2.age: = 9; showmessage (prec2.name); {} {manual memory, must be manually released} freemem (prec2); end ;// Although a pointer of the same structure, 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} {in turn, 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 procedure tform1.button4click (Sender: tobject); begin rec. name: = 'duba '; Rec. age: = 36; P: = @ REC; {value assignment} tprec (P ). name: = 'hou jiu'; {value} showmessage (tprec (P ). name); {Hou JIU} end; 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.