The definition and difference of Pchar, Char and String in Delphi

Source: Internet
Author: User

Char is a single character;
PChar is a pointer to a null-terminated string with each character as a byte. (Can be seen as a string)
String is an array of characters, ending with a character #0;


Char occupies only one byte of space, while Pchar holds a pointer to the size of 1 pointer bytes in the current 32-bit system.
Type Pchar=^char;

The Pchar type is a pointer to char and uses the extended syntax of Delphi. It can also be treated as a string or as a pointer to a char array.

Use of Pchar

The most common way to use 1.PChar is as a DLL written in C or C + +, such as a parameter type for Windows API.
2. You can treat a pchar as a pointer to a char array. Array subscript is the sub-range of integer, starting from zero. Delphi does not provide any bounds checking for this array. Usually the end of the string is represented by the #0 character.
3. Arithmetic operations on Pchar pointers are usually done by adding and subtraction integers similar to the INC and DEC processes.

Conversion of Pchar

Long string conversions to Pchar are not automatic. The difference between them leads to problems with their conversion.

1, a long string is a reference count, and Pchar is not
2, assigning a long string is a copy of the data, and Pchar is a pointer to the data.
3, the long string is the end of the null stop character and contains the length of the string. And Pchar is the simple end of the null-stop character. (the end of the null character ends with #0)

Example

Example:
Var
Pc:pchar;
Begin
PC: = ' ABCD ' + #0 + ' efghijklmnopqrstuvwxyz ';
ShowMessage (PC); {ABCD}
End

This example shows that Pchar can be used as an array of char characters ending with #0
Transformation:
String is a defined type in Delphi, and Pchar is a pointer string with a suffix of #0.

Convert words strpas (pchar);//pchar turn into string
Strpcopy (pchar,string);
or direct Pchar (string)//string turns into Pchar

Conversion of String--> Char

Example
Procedure Tform1.button1click (Sender:tobject);
Var
Ch:array
[0..5] of char;
str:string;
I:integer;
Begin
str: = ' 123456 ';
Move (Str[1],ch,length (str));//move Description: Move (move from the number of bits of the string to the target, how many lengths or sizes to move, notice the difference between sizeof and legth)
For
I:=0 to 5 do
Edit1. Text: =edit1. Text + ch[i];//show:123456
Move (str[3],ch,4);//ch=3456
End

1. Use a pointer to a string, and if it does not end with 0, an error will occur at run time. To avoid this error, you need to manually add 0 to the end of the string
That is, char (0), or use the Strpcopy function to automatically add 0 to the end of the string.
Example 3: Assigning a value with the Strpcopy function automatically adds 0 to the end of the string S.

Var
S:array of Char; {s[0]=4 s[1]= ' n ' s[2]= ' e ' s[3]= ' W ' s[4]=0;} {p--> ' new '}
P:pchar;
Begin
P:[email Protected][1];

Strpcopy (P, ' new '); {strpcopy function automatically adds 0} at end of string
Label1.caption:=strpas (P); {New}

Label2.caption:=s; {New}
Label3.caption:=inttostr (Integer (s[0])); {4}

End

The definition and difference of Pchar, Char and String in Delphi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.