string与PChar(0分)

F

feng547

Unregistered / Unconfirmed
GUEST, unregistred user!
长字符串转换到PChar不是自动的。他们之间的不同点导致他们的转换存在问题。
1,长字符串是引用计数的,而PChar不是
2,赋值给长字符串是数据的拷贝,而PChar是指向数据的指针。
3,长字符串是空止符结尾,并包含有字符串的长度。而PChar是简单的空止符结尾。( 空止符结尾指以#0结尾)
procedure my_func(x: string);
begin
some_proc(PChar(x));
//参照第一条,这么做自己负责x的生存期
end;

function title(n: Integer): PChar;
var
s: string;
begin
s := Format('title - %d', [n]);
Result := PChar(s);
// 参照第2条,不能这么做
end;


var
s:string;
pc:pchar;
pb:pbyte;
ac:array[1..100] of char;
ab:array[1..100] of byte;
i:integer;
begin
s:='this is a test';
pc:=pchar(s);
//string->pchar
pb:=pbyte(pc);
//pchar->pbyte
for i:=1 to length(s)do
begin
ac:=s;
//string->arrary of char
ab:=byte(s);
//string->arrary of byte
end;

s:=pc;
//pchar->string
s:=string(pb);
//pbyte->string
s:=c;
//arrary of char->string;
end;
 

Similar threads

回复
0
查看
810
不得闲
S
回复
0
查看
913
SUNSTONE的Delphi笔记
S
S
回复
0
查看
743
SUNSTONE的Delphi笔记
S
I
回复
0
查看
505
import
I
I
回复
0
查看
637
import
I
顶部