Pchar,pansichar,string,ansistring,char array, Ansichar array conversion

Source: Internet
Author: User

Pchar,pansichar,string,ansistring,char Array, the conversion relationship between Ansichar arrays is shown in

Through the conversion chain, you can realize the mutual transfer between any two types. such as PChar to Pansichar, according to the conversion chain to know Dest: = Pansichar (source), the same ansistring to Pansichar as PChar dest:= (String (source))

If the conversion result is finally string, you can do without a string conversion, such as ARRAY[0..N] of Ansichar to string, the conversion chain is Dest: = Source

Thus, we can draw a conclusion

Conversions between 1.Ansi and wide need to be converted by ansistring or string

2. Array and pointer conversion as far as possible copy, such as the use of pointers to the way, you need to pay attention to change the value of the impact

3. Any type of transfer string will be copied, not pointer pointing, so the original memory changes will not affect the new value


Test code and test results attached

procedureTform1.btn1click (Sender:tobject);
var
Lansiarray:Array[0.. -] ofAnsichar;
Lchararray:Array[0.. -] ofChar;
Lpansichar:pansichar;
Lpchar:pchar;
lansistr:ansistring;
LSTR:string;
begin
Mmo1. Lines.add ('Direct Assignment Output ********');

Lansiarray: ='Ansichar Array';
Mmo1. Lines.add (Lansiarray);

Lchararray: ='Widechar Array';
Mmo1. Lines.add (Lchararray);
Lpansichar: ='Pansichar String';
Mmo1. Lines.add (Lpansichar);
Lpchar: ='Pchar String';
Mmo1. Lines.add (Lpchar);
LANSISTR: ='ansistring String';
Mmo1. Lines.add (LANSISTR);
LSTR: ='String strings';
Mmo1. Lines.add (LSTR);
Mmo1. Lines.add ("');
End;

procedureTform1.btn2click (Sender:tobject);
var
Lansiarray:Array[0.. -] ofAnsichar;
Lpansichar:pansichar;
begin
Mmo1. Lines.add ('pansichar<->ansichar[]********');

//copy Lpansichar content to Lansiarray memory
Lpansichar: ='Pansichar String';
Strcopy (Lansiarray, Lpansichar);
Mmo1. Lines.add (Lansiarray);

//allocate space for Pansichar and then copy the Ansichar array data to that space
Getmem (Lpansichar, -);
Strcopy (Lpansichar, @LAnsiArray [0]);
lansiarray[0] :='Z';
Mmo1. Lines.add (Lpansichar);
Freemem (Lpansichar);

//The Lpansichar pointer points to the ANSICHAR array, so the array changes affect the Pansichar
Lpansichar: = Lansiarray;
Mmo1. Lines.add (Lpansichar);
lansiarray[0] :='X';
Mmo1. Lines.add (Lpansichar);

Mmo1. Lines.add ("');
End;

procedureTform1.btn3click (Sender:tobject);
var
Lpansichar:pansichar;
lansistr:ansistring;
begin
Mmo1. Lines.add ('ansistring<->pansichar********');
LANSISTR: ='ansistring String';
//the pointer to the Lpansichar points to the LANSISTR address
Lpansichar: = Pansichar (LANSISTR);
//lansistr Reassign Address, assign value, no effect on Lpansichar
LANSISTR: ='zzzzzzz String';
Mmo1. Lines.add (Lpansichar);

//the LANSISTR pointer points to the address that the Lpansichar pointer points to
LANSISTR: = Lpansichar;
Mmo1. Lines.add (LANSISTR);
Mmo1. Lines.add ("');
End;

procedureTform1.btn4click (Sender:tobject);
var
Lpchar:pchar;
lansistr:ansistring;
begin
Mmo1. Lines.add ('ansistring<->pcharr********');
Lpchar: ='Pchar String';
LANSISTR: = ansistring (Lpchar);
Mmo1. Lines.add (LANSISTR);

Lpchar: = PChar (string(LANSISTR));
Mmo1. Lines.add (Lpchar);
End;

procedureTform1.btn5click (Sender:tobject);
var
Lansiarray:Array[0.. -] ofAnsichar;
Lchararray:Array[0.. -] ofChar;
Lpansichar:pansichar;
Lpchar:pchar;
lansistr:ansistring;
LSTR:string;
begin
Mmo1. Lines.add ('all->string********');

Lansiarray: ='Ansichar Array';
//This is a copy operation, not a pointer pointing, so changing the array value has no effect on LSTR
LSTR: = Lansiarray;
lansiarray[0] :='X';
Mmo1. Lines.add (LSTR);

Lchararray: ='Widechar Array';
//This is a copy operation, not a pointer pointing, so changing the array value has no effect on LSTR
LSTR: = Lchararray;
lchararray[0] :='X';
Mmo1. Lines.add (LSTR);

Lpansichar: ='Pansichar String';
LSTR: = Lpansichar;
Mmo1. Lines.add (LSTR);

Lpchar: ='Pchar String';
//This is a copy operation, not a pointer pointing to
LSTR: = Lpchar;
Mmo1. Lines.add (LSTR);

LANSISTR: ='ansistring String';
//This is a copy operation, not a pointer pointing to
LSTR: = Lansistr;
Mmo1. Lines.add (LSTR);

Mmo1. Lines.add ("');
End;

procedureTform1.btn6click (Sender:tobject);
var
lansistr:ansistring;
LSTR:string;
begin
Mmo1. Lines.add ('string->ansistring********');
LSTR: ='String strings';
LANSISTR: = ansistring (LSTR);
Mmo1. Lines.add (LANSISTR);
Mmo1. Lines.add ("');
End;

procedureTform1.btn7click (Sender:tobject);
var
Lpchar:pchar;
LSTR:string;
begin
Mmo1. Lines.add ('string->pchar********');
LSTR: ='String strings';
Lpchar: = PChar (LSTR);
Mmo1. Lines.add (Lpchar);
Mmo1. Lines.add ("');
End;

procedureTform1.btn8click (Sender:tobject);
var
Lchararray:Array[0.. -] ofChar;
LSTR:string;
begin
Mmo1. Lines.add ('string->char[]');
LSTR: ='String strings';
Strcopy (Lchararray, PChar (LSTR));
Mmo1. Lines.add (Lchararray);
Mmo1. Lines.add ("');
End;

procedureTform1.btn9click (Sender:tobject);
var
Lchararray:Array[0.. -] ofChar;
Lpchar:pchar;
begin
Mmo1. Lines.add ('pchar<->char[]********');

//copy Lpchar content to Lchararray memory
Lpchar: ='Pchar String';
Strcopy (Lchararray, Lpchar);
Mmo1. Lines.add (Lchararray);

//allocates space to the Pchar, and then copies the char array data to the space
Getmem (Lpchar, -);
Strcopy (Lpchar, @LCharArray [0]);
lchararray[0] :='Z';
Mmo1. Lines.add (Lpchar);
Freemem (Lpchar);

//The Lpchar pointer points to a char array, so array changes affect Pchar
Lpchar: = Lchararray;
Mmo1. Lines.add (Lpchar);
lchararray[0] :='X';
Mmo1. Lines.add (Lpchar);

Mmo1. Lines.add ("');
End;

procedureTform1.btn10click (Sender:tobject);
var
Lchararray:Array[0.. -] ofChar;
Lpchar:pchar;
LSTR:string;
begin
Lchararray: ='Char Array';
Lpchar: = Lchararray;
//Pchar turns a string, does a copy, not a pointer to
LSTR: = Lpchar;

Mmo1. Lines.add ('char[]:'+ Lchararray);
Mmo1. Lines.add ('PChar:'+ Lpchar);
Mmo1. Lines.add ('String:'+ lstr);

//change the value of an array
lchararray[0] :='X';

Mmo1. Lines.add ('char[]:'+ Lchararray);
Mmo1. Lines.add ('PChar:'+ Lpchar);
Mmo1. Lines.add ('String:'+ lstr);

End;

procedureTform1.btn11click (Sender:tobject);
var
Lpchar:pchar;
Lpansichar:pansichar;
begin
Mmo1. Lines.add ('Pchar<->pansichar');
Lpansichar: ='Pansichar String';
Lpchar: = PChar (String (Lpansichar));
Mmo1. Lines.add (Lpchar);

Lpchar: ='Pchar String';
Lpansichar: = Pansichar (ansistring (Lpchar));
Mmo1. Lines.add (Lpansichar);

Mmo1. Lines.add ("');
End;

Pchar,pansichar,string,ansistring,char array, Ansichar array conversion

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.