First, the definition of type pointer. For a specific type of pointer, it is defined in C:
INT * PTR;
Char * PTR;
How is the equivalent Object Pascal defined?
VAR PTR: ^ integer;
PTR: ^ char;
In fact, it is the difference in symbols.
Second, there is no definition of type pointer. There is a void * type in c, which is to point to pointers of any type of data. Object Pascal defines a special type: Pointer. then,
PTR: POINTER;
In c
Void * PTR;
Equivalent.
Third, the release of the pointer. To release the pointer reference (ie, the value of the area refers to the area refers to the pointer), the syntax of C is (* PTR), Object Pascal is Ptr ^.
Fourth, take the address (the pointer assignment). Take an object's address and assign it to the pointer variable, the syntax of C is
PTR = & object;
Object Pascal is
PTR: = @Object;
It is only the difference in symbols.
Five, pointer operation. In C, the computing can be moved, such as:
Char a [20];
Char * ptr = a;
PTR ;
PTR = 2;
When PTR is performed; the compiler generates code that allows PTR to advance the sizeof (char) step, then PTR will point to A [1]. PTR = 2; This sentence makes the PTR forward into two SizeOf (char) steps. Similarly, let's take a look at how the object pascal implements:
VAR A: Array [1..20] of char;
PTR: PCHAR; // pchar can be seen as ^ char
Begin
PTR: = @A;
Inc (PTR); // This sentence is equivalent to PTR ;
Inc (PTR, 2); // This sentence is equivalent to PTR = 2;
END;
Sixth, dynamic memory allocation. In the C language, use the malloc () library function allocated memory, free () function releases memory. Such a code:
INT * PTR, * PTR2;
INT I;
PTR = (int *) malloc (sizeof (int) * 20);
PTR2 = PTR;
For (i = 0; i <20; i ) {
* PTR = I; PTR ;
}
Free (PTR2);
In Object Pascal, the function of dynamically assigning the memory is getMem (), and the corresponding release function is freeMem () (more functions in traditional pascal is new () and dispose (), but new () can only get the object The memory size of a single entity cannot obtain a continuous memory block of multiple objects. Therefore, the code equivalent to the code equivalent of the C code above is:
VAR PTR, PTR2: ^ integer;
i: integer;
Begin
GetMem (PTR, SIZEOF (Integer);
// This sentence is equivalent to PTR = (int *) malloc (sizeof (int * 20);
Ptr2: = Ptr; // Reserved the original pointer position
For i: = 0 to 19 do
Begin
PTR ^: = I;
Inc (PTR);
END;
FreeMem (PTR2);
END;
For the above example (whether C versions, or Object Pascal version), pay attention to a problem, that is, the unit allocated memory is byte, so when using getMem, if the second parameter is of course Write into 20, then there will be a problem (memory access crosses). Because GetMem (PTR, 20); actually only assigned 20 bytes of memory space, and a shaping size is four bytes, then all elements after accessing the fifth are illegal (for malloc () The parameters are the same). 7. The operation of the character array. In the C language, there is no string type, so the strings are implemented with a character number, so there is also a set of STR header library functions to perform the array of characters, such as the following code:
Char Str [15];
CHAR * PSTR;
STRCPY (STR, "Teststr");
STRCAT (STR, "_Testok");
PSTR = (char *) Malloc (sizeof (char) * 15);
STRCPY (PSTR, STR);
Printf (PSTR);
Free (PSTR);
In Object Pascal, there is a String type, so it can make a variety of computments for strings. However, sometimes our Pascal code needs to interact with C (such as: Call C written with the CSCAL code, or written with the CSCAL, you can use the Cerect Pascal code), you cannot use the String type. And you must use two languages universal characters. In fact, Object Pascal provides an operational function of a full-character number of characters similar to C, which is the option of the Object Pascal version of the above code:
Var Str: array [1..15] of char;
PSTR: PCHAR; // pchar is also ^ char
Begin
Strcopy (@str, 'teststr');
// In C, the name of the array can be used directly as the first address pointer
/ / But Pascal is not like this, so before the STR is adding the operator to take the address.
STRCAT (@str, '_testok');
GetMem (PSTR, SIZEOF (CHAR) * 15);
Strcopy (PSTR, @str);
Write (PSTR);
FreeMem (PSTR);
END;
Eight, function pointer. When a function in Dynamic calls the DLL, the function pointer is used. Assume that a code written with C is as follows:
typedef int (* pvfn) (int); // Define function pointer type
int main ()
{
HModule Hmodule = loadingLibrary ("Test.dll");
PVFN PVFN = NULL;
PVFN = (PVFN) GetProcaddress (HMODULE, "Function1");
PVFN (2);
Freelibrary (hmodule);
}
As far as I personally feel, the syntax of the TypeDef code that defines the function pointer type in the C language, and the same code is very easy to understand in Object Pascal:
TYPE PVFN = Function (Para: Integer): Integer;
VAR FN: PVFN;
// can also be defined herein, such as: Fn: Function: integer; hm: hmodule;
Begin
HM: = loadingLibrary ('Test.dll');
Fn: = getProcaddress (HM, 'Function1');
Fn (2);
Freelibrary (HM);
END;