Pointer about Delphi

zhaozj2021-02-16  53

because

Delphi

The data type is very convenient, I have never used a pointer, but the recent software needs to use the list, so I have to learn.

Delphi

Pointer. Don't know if you use it, you will find any introduction.

Delphi

Books have avoided the problem of the pointer, and it is difficult to find a picture online.

Object Pascal

Pointer. Although the article

Delphi

Pointer and

C / C

It has been compared learning, but the practical example is too small, so after writing the program, write some of the use procedures.

Now suppose that our linked list is only a variable I, then the structure in C / C is defined as follows:

Struct TTest

{

INT I;

Struct TTEST * NEXT;

}

I wrote a structure according to the definition of C.

TYPE TTEST = Record

i: integer;

Next: ^ TTEST;

END;

Try it once, error in Next: ^ TTEST; (Type undefined) The method is a roundabout. After another study, "Talking about Object Pascal" has found that there is a non-type pointer Pointer to replace ^ TTest to Pointer. Compiled. Since the NEXT pointer is a non-type pointer, it must first assign this pointer to a type pointer to control the structural variables pointed to the pointer.

With structural, next, I have designed a queue tQueue that is implemented with a chain list. (The class given here is simplified)

Type

Tqueue = Class

Private

First, last: ^ TTEST

Public

Procedure add (s: integer);

END;

Procedure tQueue.add (s: integer)

VAR

P: ^ TTEST;

Begin

Getmam (p, sizeof (ttest));

P.i: = i;

p.NEXT: = NIL;

Last.next: = P;

Last: = P;

END;

Add () is to add a data in the queue. It first uses the getman function to get a memory, then assign a value, and the newly allocated variable is hung into the end of the linked list. It looks very simple, actually writing code is also very simple, but the compiler will not pass it. In Last: = P Here you prompt the variable type inconsistent. I retrieved the code again, and for finding any problems, it is defined as ^ Ttext's pointer, why can't you! I am a stay in the screen for a while, I decided to go back to the battle. This time, the first, last is defined as a non-type pointer Pointer, and compiled again. However, there is a need to make some corresponding changes to add (), which adds a temporary structure variable T.

Procedure tQueue.add (s: integer)

VAR

T, P: ^ TTEST;

Begin

Getmam (p, sizeof (ttest));

P.i: = i;

p.NEXT: = NIL;

T: = Last;

T.NEXT: = P;

Last: = P;

END;

转载请注明原文地址:https://www.9cbs.com/read-19779.html

New Post(0)