天天看點

delphi 函數和指針_在Delphi中了解和使用指針

delphi 函數和指針

Even though pointers aren't as important in Delphi as they are in C or C++, they're such a "basic" tool that almost anything having to do with programming must deal with pointers in some fashion.

盡管指針在Delphi中不如在C或C ++中那麼重要,但它們是一個“基本”工具,幾乎所有與程式設計有關的事情都必須以某種方式處理指針。

It's for that reason that you might read about how a string or object is really just a pointer, or that an event handler such as OnClick, is actually a pointer to a procedure.

是以,您可能會讀到關于字元串或對象實際上隻是指針的資訊,或者事件處理程式(例如OnClick)實際上是指向過程的指針。

指向資料類型的指針 ( Pointer to Data Type )

Simply put, a pointer is a variable that holds the address of anything in memory.

簡而言之,指針是一個變量,用于儲存記憶體中任何内容的位址。

To concrete this definition, keep in mind that everything used by an application is stored somewhere in the computer's memory. Because a pointer holds the address of another variable, it's said to point to that variable.

為了具體化此定義,請記住,應用程式使用的所有内容都存儲在計算機記憶體中的某個位置。 因為指針儲存着另一個變量的位址,是以據說它指向該變量。

Most of the time, pointers in Delphi point to a specific type:

大多數情況下,Delphi中的指針指向特定類型:

var
iValue, j : integer;pIntValue : ^integer;
beginiValue := 2001;pIntValue := @iValue;...j:= pIntValue^;

The syntax to declare a pointer data type uses a caret (^). In the above code, iValue is an integer type variable and pIntValue is an integer type pointer. Since a pointer is nothing more than an address in memory, we must assign to it the location (address) of the value stored in the iValue integer variable.




The @ operator returns the address of a variable (or a function or procedure as will be seen below). Equivalent to the @ operator is Addr function. Note that pIntValue's value is not 2001.




In this sample code, pIntValue is a typed integer pointer. Good programming style is to use typed pointers as much as you can. The Pointer data type is a generic pointer type; it represents a pointer to any data.




Note that when "^" appears after a pointer variable, it de-references the pointer; that is, it returns the value stored at the memory address held by the pointer. In this example, variable j has the same value as iValue. It might look like this has no purpose when we can simply assign iValue to j, but this piece of code lies behind most calls to Win API.


         

NILing Pointers

Unassigned pointers are dangerous. Since pointers let us work directly with computer's memory, if we try to (by mistake) write to a protected location in memory, we could get an access violation error. This is the reason we should always initialize a pointer to NIL. NIL is a special constant that can be assigned to any pointer. When nil is assigned to a pointer, the pointer doesn’t reference anything. Delphi presents, for example, an empty dynamic array or a long string as a nil pointer.

Character Pointers

The fundamental types PAnsiChar and PWideChar represent pointers to AnsiChar and WideChar values. The generic PChar represents a pointer to a Char variable. These character pointers are used to manipulate null-terminated strings. Think of a PChar as being a pointer to a null-terminated string or to the array that represents one.

Pointers to Records

When we define a record or other data type, it's a common practice also to define a pointer to that type. This makes it easy to manipulate instances of the type without copying large blocks of memory. The ability to have pointers to records (and arrays) makes it much easier to set up complicated data structures as linked lists and trees.
翻譯自: https://www.thoughtco.com/understanding-and-using-pointers-in-delphi-1058219

delphi 函數和指針