Delphi Pointers easy to get started

Source: Internet
Author: User

Simple Introduction to Delphi pointers:

See an example of a pointer usage:
1 var
2 X, Y:integer; X and Y integer types
3 P: ^integer; P pointer to integer type
4 begin
5 X: = 17; Assign a value to X
6 P: = @X; Assign the address of X to P
7 Y: = p^; Remove the value pointed to by P to assign Y
8 End;

The second row defines the two variables x, Y. The third line declares that p is a pointer to an integer type, meaning that p can point to the address of X or Y. The fifth line assigns the X value, and the sixth line assigns the address of X to P. most

Value to Y after the variable p is pointed to. At this point, X and Y have the same value.

The operator @ is used to take out the address of the variable, or the address of the procedure and function.

and the symbol ^ has two goals,
When it appears in front of a type definition, such as ^typename indicates pointers to this type;
When it appears behind a pointer variable, such as point^ returns the value of the variable pointed to by the pointer;

Understanding pointers makes it easier to understand the object-oriented Pascal language, because pointers are often manipulated behind the scenes. Any type that requires dynamic allocation of large memory spaces can be used with pointer types. For example,

, the long-string variable is actually manipulated using pointers. Other advanced programming techniques require the use of pointer types.
Sometimes pointers are the only way to adapt to the strict type restriction of Object Pascal. A generic pointer type, converted to a different pointer type by type, as in the following example:
Type

Pinteger = ^integer;
Var
R:single;
I:integer;
P:pointer; Generic pointers
Pi:pinteger;
Begin
P: = @R; Remove the memory address of R
PI: = Pinteger (P); Converts a generic type to a pointer to an integer type
I: = pi^;
End

Of course, real numbers and integers are stored in different formats. This assignment is to copy the original binary data from R to I without converting it.

The reserved word nil is a special constant that can be assigned to any pointer type, and when nil assigns a pointer, the pointer does not point to anything, it is a null pointer.

The @ operator returns the stored address of the variable in memory, or a procedure/function/method;

1. If the variable, @x returns the address of X. If the compile option {$T-} is not open, the return thing is a generic pointer, if the compile option is turned on, the return is the type of x corresponding to the reference

Needle.

2. If it is a routine (procedure/function), @f returns the entry point of F, and the type of @f is a pointer.

3. When @ is used in a method of a class, the name of the method must have a class name, such as @tmyclass.dosomething
The pointer points to the DoSomething method of the Tmyclass.


When a process variable is on the left side of an assignment statement, the compiler expects a procedure value to the right of the assignment statement. This assignment allows the left-hand variable to point to the procedure or function defined on the right.

Entry point. In other words, the variable can be used to refer to a declared procedure or function, and you can directly use a reference to the argument.

Var

F:function (X:integer): Integer;
I:integer;
function SomeFunction (X:integer): Integer;
...
F: = SomeFunction; Assign a value to F
I: = F (4); Call the function you are pointing to

In an assignment statement, the type of the left variable determines the interpretation of the procedure or method pointer on the right.

Var

F, G:function:integer;
I:integer;
function Somefunction:integer;
...
F: = SomeFunction; Assign a value to F
G: = F; Copy the value of F to G
I: = G; Calling functions

The first sentence gets the entry of the function, the second sentence copies the pointer, and the third sentence gets the return value of the function.

Sometimes it's possible to use
If F = MyFunction then ...;
Here, the presence of f causes a function call; The compiler calls the function f points to, and then calls MyFunction, comparing the results. This rule is whenever a process variable (

Procedural variable) appears in an expression that represents the function or procedure to which the call is directed. Sometimes f points to a process (no return value), or F points to a

Number of functions, the preceding statement produces a compilation error. To compare F and myfunction, you need to use
If @F = @MyFunction then ...;
@f converts f to an untyped pointer variable that contains an address, @myfunction returns the address of the myfunction.
Get the memory address of a process variable using @@. For example, @@f returns the address of F.

The @ operator usually assigns an untyped pointer value to a procedure variable, for example:
var strcomp:function (STR1, Str2:pchar): Integer;
...
@StrComp: = GetProcAddress (Kernelhandle, ' lstrcmpi ');
Call the Getprocaddres function, and use StrComp to point to the value
Any process variable can be assigned nil, indicating that nothing is pointed. However, a procedure variable that attempts to invoke a nil value causes an error in order to test whether a procedure variable can be assigned a value

, using the standard assignment function assigned
If Assigned (onclick) then OnClick (X);


First come here, I am also looking for help while translating, no reference to what books, so there are some nouns may not be too accurate, I hope you can forgive me, after all, my English proficiency is limited AH

. hehe

What problems can be seen in Delphi's help related content, personally think or see help better, and the original is the best, like I translate may mislead you, look at the time to be affected by its essence

Discard its dross, read not scold me on the line, all when I give everyone still a brick bar, do not hit me ah @[email protected]

http://blog.csdn.net/diligentcatrich/article/details/5492963

Delphi Pointers easy to get started

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.