⒈ ⒈ ⒈ type statement
Pointer type declaration
The pointer type is more difficult to understand in any language is also a more flexible data type, and the pointer is usually the memory address of the variable it refers to.
The grammar of the declared pointer is as follows:
TYPE pointer type identifier = ^ Class Type;
The program is now as follows:
Type
Iteptr = ^ Byte;
Wordptr = ^ Word;
In the above example, two pointer types are declared, one is byteptr, pointing to the Byte type data, the other is WordPtr, pointing to the Word type data.
Object Pascal does not mean that the base type must be declared in the previous, or only an identifier, then declare the base type within the same block.
After declaring the type of pointer, you can declare the variable of the pointer type, for example:
VAR
BP: byteptr;
WP: WordPtr;
To simplify the program, you can also merge the declaration of the type of declaration and variables together, for example:
VAR BP: ^ Byte;
The base type referred to by the pointer can be a simple type or a structure type, for example:
TYPE Student = Record
Name: String;
Atte: integer;
SEX: (MAN, Woman);
END;
Var stuptr: ^ student;
In the above example, a pointer variable constudptr pointing to the record type Student is declared, and the record class can be represented by STUPTR ^ in the latter program.
Dynamic variables of STUDENT, to access the Name fields, can be written as stuptr ^ .name.
Here, the concept of dynamic variables is introduced, and the dynamic variable is constituted, and one ^ symbol is added with the pointer type variable identifier, it constitutes the pointer.
Dynamic variables of the base type pointed to the above examples are the dynamic variables of stuxt in the above example.
(In the Delphi program, many programs are used to doing so.
TYPE PSTUDENT = ^ TSTUDENT;
TSTUDENT = Record
Name: String;
Atte: integer;
SEX: (MAN, Woman);
END;
)
⒉ ⒉ ⒉
In fact, the simple usage of the pointer has been introduced, and several processes and operators for pointer operation are also introduced.
NEW is the standard routine in Object Pascal, which is used to assign dynamic variables in the stack of applications.
The address of the area is assigned to the pointer variable, and the size of the assigned area is determined by the type referred to by the pointer. If the application's stack is not enough
Enough space will trigger an EoutofMemory exception.
The declaration of the New process is as follows:
Procedure New (VAR P: POINTER);
Where P is a pointer variable, after the New process is called, the program can use the P ^ as the dynamic variable of the type of pointer.
Accordingly, when the program is no longer needed to use a dynamic variable, the standard routine Dispose should call the dynamic variable created by NEW and release it.
Allocated space.
The program is now as follows:
Type
Plistentry = ^ TListentry;
TLISTENTRY = Record
Next: PLISTENTRY;
Text: String;
Count: integer;
END;
VAR
List, p: plistentry;
Begin
... ...
P ^ .text: = 'Hello World';
P ^ count: = 1;
List: = P;
Dispose (P);
... ...
END;
In the above example, first declare a pointer type plistentry, pointing to the TListentry type, the Declaration of the TListentry type is below, is a
Record type. Then declare two PListentry type variables List and P.
There is also a routine getMem in Object Pascal, which is similar to New, and also assigns a block for dynamic variables in the stack of applications.
Domain, and assign the address of the area to the pointer variable, the GetMem routine specifies the size of the area to allocate the area, the declaration of getMem is as follows:
Procedure getmem (var P: Pointer; SIZE: Integer);
Where P is the pointer variable, size is byte.
Accordingly, the dynamic variable created with getmem must be deleted with the FreeMem routine and release the assigned space.
There is also an @ operator in the Object Pascal language, used to get the address of the operand, the operand can be variables, processes, functions, or class types.
Method, please refer to the operation about @ 操作 操作
symbol.
There is a special reserved word nil in Object Pascal, which is an empty pointer, when the value of the pointer is NIL, indicating that the pointer does not
To any dynamic variable. Note: You cannot use a value of NIL to change
Quantity to access dynamic variables.
In addition to the value of the pointer, the pointer variable can also be equally or unequal comparison, which is limited to the type-compatible pointer variable. In two
The pointer points to the same object (not the same type) pointer
equal.
⒊ None type pointer
None type pointer is pointed to the pointer variable declaration does not have a base type, and there is no type of pointer to declare:
Var PTR: POINTER;
The role of the non-type pointer is that it can point to any type, but for the non-type pointer, you cannot use similar PTR ^ to reference it.
Dynamic variable.