Object Pascal pointer
1. Type pointer definition.
For pointers pointing to specific types, the definition in C is as follows:
Int
* PTR;
Char * PTR;
How is the equivalent Object Pascal defined?
VaR
PTR: ^ integer;
PTR: ^ char;
It is actually the difference between symbols.
2. No type pointer definition.
Void in C
* Type, that is, the pointer that can point to any type of data.
In Object Pascal, a special type is defined for it: pointer. So,
PTR:
Pointer;
In C
Void
* PTR;
Equivalent.
3. unreference the pointer.
To remove the pointer reference (that is, to retrieve the value of the region indicated by the pointer ),
C syntax is
(* PTR), Object Pascal is PTR ^.
4. Get the address (assign a pointer ).
Take the address of an object and assign it to the pointer variable, c
The syntax is
PTR = & object;
Object Pascal is
PTR: =
@ Object;
It is only a symbol difference.
5. pointer operation.
In C, the pointer can be moved, for example:
Char
A [20];
Char
* PTR =;
PTR ++;
PTR + = 2;
When executing PTR ++;, the compiler will generate code that allows PTR to move forward the sizeof (char) Step. After that, PTR will point to a [1].
PTR + = 2; this statement makes the PTR move forward to two sizeof (char) step sizes.
Let's take a look at the object
How to Implement in Pascal:
VaR
A: array [1 .. 20]
Char;
PTR: pchar; // pchar can be viewed
^ Char
Begin
PTR: = @;
INC (PTR );//
This sentence is equivalent to C's PTR ++;
INC (PTR, 2); // This sentence is equivalent to C's
PTR + = 2;
End;
6. Dynamic Memory Allocation.
In C, use the malloc () library function to allocate memory, and the free () function to release the memory. Code like this:
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 the object
In PASCAL, the function for dynamically allocating memory is getmem (), and the corresponding release function is freemem ()
(In traditional Pascal, the functions used to obtain memory are new () and
Dispose (), but new () can only obtain the memory size of a single object of an object,
Cannot obtain memory blocks that store multiple objects consecutively ). Therefore, the object equivalent to the above section C code
Pascal's code is:
VaR PTR, ptr2: ^ integer;
I:
Integer;
Begin
Getmem (PTR, sizeof (integer) * 20 );
// This sentence is equivalent to C's PTR = (int *) malloc (sizeof (INT )*
20 );
Ptr2: = PTR; // retain the original pointer position
For I: = 0 to 19
Do
Begin
PTR ^: =
I;
INC (PTR );
End;
Freemem (ptr2 );
End;
For the above example (whether in C or object
Pascal version), you must pay attention to one problem, that is, the unit of memory allocation is byte. Therefore, when using getmem, if the second parameter is written
20, then there will be a problem (memory access out of bounds ). Because getmem (PTR,
20); actually, only 20 bytes of memory space is allocated, and the size of an integer is four bytes, so all elements after the fifth access are invalid (for malloc () ).
7. Operations on character Arrays.
In C, there is no string type. Therefore, strings are all implemented using character arrays,
As a result, there is also a set of database functions with str headers for the calculation of character arrays, as shown in 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 the object
Pascal has the string type, so you can easily perform various operations on the string. However, sometimes our Pascal code needs to interact with C Code (for example, using object
Pascal's code calls the DLL written in C or uses the object
If the DLL written by Pascal is ready to allow C to write the client code), the string type cannot be used, but a string array must be used in both languages. In fact, Object
Pascal provides a complete set of arithmetic functions similar to C character arrays. The Object Pascal version of the code above is as follows:
VaR STR:
Array [1 .. 15] of char;
Pstr: pchar; // pchar, that is
^ Char
Begin
Strcopy (@ STR, 'teststr ');
// In C, the array name can be directly used as the first address pointer of the array.
// But Pascal does not. Therefore
Add the bitwise operator before Str.
Strcat (@ STR,
'_ Testok ');
Getmem (pstr, sizeof (char )*
15 );
Strcopy (pstr,
@ Str );
Write (pstr );
Freemem (pstr );
End;
8. function pointer.
When you call a function in a DLL dynamically, the function pointer is used. Assume that a piece of code written in C is as follows:
Typedef
INT (* pvfn) (INT); // defines the function pointer type.
Int
Main ()
{
Hmodule =
Loadlibrary ("test. dll ");
Pvfn = NULL;
Pvfn = (pvfn)
Getprocaddress (hmodule,
"Function1 ");
Pvfn (2 );
Freelibrary (hmodule );
}
In my personal sense, the syntax of the typedef Code defining the function pointer type in C language is somewhat obscure,
The same code is used in the object
Pascal is very easy to understand:
Type pvfn = function (para: integer ):
Integer;
VaR
FN: pvfn;
// It can also be defined directly here, for example: FN: Function
(Para: integer): integer;
HM:
Hmodule;
Begin
HM: =
Loadlibrary ('test. dll ');
FN: = getprocaddress (HM,
'Function1 ');
FN (2 );
Freelibrary (HM );
End;
The above is reproduced from:
Http://www.programfan.com/blog/article.asp? Id = 863