Delphi type and pointer

Source: Internet
Author: User

 

Types and pointers of Delphi [1]

First declare: do not regard the things I have mentioned as textbooks. They are all my own opinions and hope to be corrected.

Delphi pointers are classified into two types: "type Pointer" and "No type Pointer.
In Delphi, there must be several hundred common types. We can define corresponding type pointers for each type.
In fact, Delphi has predefined pointers for many types, such as data types:
Integer has the corresponding pinteger;
Char has the corresponding pchar;
String has the corresponding pstring;
For example:
The tpoint has a corresponding Ppoint;
Tcolor has pcolor and so on.

In addition, pointers can also have pointers. For example, pchar is a character pointer and ppchar is a pchar pointer (all of which are pre-defined by Delphi ).

Based on the example above, we will first summarize the naming rules for types and pointers:
Type Conventions use t headers (except for conventional Delphi data types, such as string );
The pointer Convention uses P as the header;
The pointer Convention for pointers is to use PP headers.
The type and pointer are two concepts that cannot be divided, and the pointer itself is also a type-"pointer type ".

First, let's look at the pointer-related operators (@, ^, ADDR ):

@ @ Variable Get variable pointer
ADDR ADDR (variable)
^ Pointer ^ Obtains the actual data pointed to by the pointer.
VaR pxxx: ^ type Variable that defines a certain type of pointer of pxxx
Type pxxx = ^ type Define pxxx as a pointer of some type

Example:

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: tform1; implementation {$ R *. DFM} // integer and pintegerprocedure tform1.button1click (Sender: tobject); var INT: integer; pint: pinteger; {define type pointer, integer type pointer} begin INT: = 100; pint: = @ int; {now pint is the pointer of int} pint ^: = pint ^ + 1; {now pint ^ is the same as int, test it :} showmessage (inttostr (INT); {101} showmessage (inttostr (pint ^); {101} end; // directly define the type pointer procedure tform1.button2click (Sender: tobject ); vaR INT: integer; pmyint: ^ integer; begin INT: = 100; pmyint: = ADDR (INT); {This sentence is the same as pmyint: = @ int;} pmyint ^: = pmyint ^ + 1; showmessage (inttostr (INT); {101} showmessage (inttostr (pmyint ^); {101} end; // define the pointer type procedure tform1.button3click (Sender: tobject); Type pint = ^ integer; var INT: integer; pmyint: pint; begin INT: = 100; pmyint: = @ int; pmyint ^: = pmyint ^ + 1; showmessage (inttostr (INT); {101} showmessage (inttostr (pmyint ^); {101} end; // pointer procedure tform1.button4click (Sender: tobject); var INT: integer; pint: pinteger; ppint: ^ pinteger; begin INT: = 100; pint: = @ int; ppint: = @ pint; ppint ^: = ppint ^ + 1; showmessage (inttostr (INT); {101} showmessage (inttostr (pint ^ )); {101} showmessage (inttostr (ppint ^); {101} end; end.
  

You can understand other people's code by knowing the above, but to thoroughly understand what the pointer is, you need to talk about it from the memory.

 


 

 

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.