PChar 表示指针,它指向一个以0结尾的Char字符数组 ?? 这个不太理解, 假设字符数组有5个字符,而第3个字符是0,那PChar不是错了?望大侠解释。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 var p:PChar;begin p:='abc'+#0+'asdfds'; showmessage( string( p ) );end; 我记得pchar是以NULL结尾的吧!可能记错,你再查查相关资料吧! 1、pchar是一个指针,指向的是一个char类型的字符2、数组的第一个元素的指针,是数组的指针如有:a : array [0..254] of char;p,p0 : pcahr;p := @a;//数组的指针p0 := @a[0];//指向数组的第一个元素的指针@a与@a[0]等价,p与p0代表了同一个东西 还是对delphi的PChar,string 以及字符串的操作不明白。等待更一步的解释, 数组与字符串内容的互相转换规则作者:小冬 (kend) 日期:2007-11-21原文出处:http://myvnet.com/article.asp?id=13http://myvnet.com/http://www.cnpack.org/数组与字符串内容的互相转换往往采用Move或CopyMemory来进行,我总结了一下它们的规则:1.Move时如果访问的是string或string[n],要用string[1],因为string[0]是字符串的长度,所以所有string都以string[1]开始。 2.Move时如果访问的是array,静态array(array[0..n] of Char)时直接使用,动态array(array of Char)时使用array[0]。因为动态array时变量名其实只是一个指针地址,不代表数组内容的地址。 3.Move时如果访问的是PChar,要使用^取指向的内容。 Move时如果目标是数组的话:1.静态数组array[0..n] of Char,取两者最小长度直接Move。2.动态数组array of Char,取string长度并把数组SetLength后再Move 如果目标是string,无论是string或string[n]都要先SetLength。如果目标是PChar,要用GetMemory申请内存,使用完后可别忘了FreeMemory。(以上string指AnsiString,string[n]指ShortString)下面是简单的一点测试代码,我没有添加结果输出,大家可以自己用F8跟踪看结果:// 字符串到数组的转换procedure StringToArray;var s1: string; s2: string[20]; buf1: array[0..19] of Char; buf2: array of Char; len1, len2: Integer; procedure cls; begin s1 := ''; s2 := ''; buf1 := ''; SetLength(buf2, 0); SetLength(buf2, 20); len1 := 0; len2 := 0; end;begin // string to char array cls; s1 := 'http://MyvNet.com'; len1 := Length(s1); len2 := Length(buf1); Move(s1[1], buf1, Min(len1, len2)); // string to dynamic char array cls; s1 := 'http://MyvNet.com'; len1 := Length(s1); len2 := Length(buf2); Move(s1[1], buf2[0], Min(len1, len2)); // string array to char array cls; s2 := 'http://MyvNet.com'; len1 := Length(s2); len2 := Length(buf1); Move(s2[1], buf1, Min(len1, len2)); // string array to dynamic char array cls; s2 := 'http://MyvNet.com'; len1 := Length(s2); len2 := Length(buf2); Move(s2[1], buf2[0], Min(len1, len2)); cls;end;// 数组到字符串的转换procedure ArrayToString;var s1: string; s2: string[20]; buf1: array[0..19] of Char; len1, len2: Integer; procedure cls; begin s1 := ''; s2 := ''; buf1 := ''; SetLength(buf2, 0); SetLength(buf2, 20); len1 := 0; end;begin // char array to string cls; buf1 := 'http://MyvNet.com'; len1 := Length(buf1); SetLength(s1, len1); Move(buf1, s1[1], len1); // char array to string array cls; buf1 := 'http://MyvNet.com'; len1 := Length(buf1); SetLength(s2, len1); Move(buf1, s2[1], len1); // or //Move(buf1, s2[1], len1); //s2[0] := Chr(len1); // dynamic char array to string cls; buf1 := 'http://MyvNet.com'; Move(buf1, buf2[0], Length(buf1)); len1 := Length(buf2); SetLength(s1, len1); Move(buf2[0], s1[1], len1); // dynamic char array to string array cls; buf1 := 'http://MyvNet.com'; Move(buf1, buf2[0], Length(buf1)); len1 := Length(buf2); SetLength(s2, len1); Move(buf2[0], s2[1], len1); // or //Move(buf2[0], s2[1], len1); //s2[0] := Chr(len1); cls;end;// PChar和数组的转换procedure TForm1.PCharAndArray;var pc: PChar; buf1: array[0..19] of Char; buf2: array of Char; len1: Integer; procedure cls; begin pc := ''; buf1 := ''; SetLength(buf2, 0); SetLength(buf2, 20); len1 := 0; end;begin // PChar to array cls; pc := 'http://MyvNet.com'; len1 := Length(pc); Move(pc^, buf1, len1); // PChar to dynamic array cls; pc := 'http://MyvNet.com'; len1 := Length(pc); Move(pc^, buf2[0], len1); // array to PChar cls; buf1 := 'http://MyvNet.com'; len1 := Length(buf1); pc := GetMemory(len1); Move(buf1, pc^, len1); FreeMemory(pc); // dynamic array to PChar cls; buf1 := 'http://MyvNet.com'; Move(buf1, buf2[0], Length(buf1)); len1 := Length(buf2); pc := GetMemory(len1); Move(buf2[0], pc^, len1); FreeMemory(pc); cls;end; 关于多线程的问题 统计字符串的问题! 怎样打开一个windows窗口? 如何使外网和局域网内机器的及时通讯? 当COM组件连接失败,如何设置能让他自动关闭 小小空值问题!!在adoquery中怎么使用EDIT做为参数值???? 在线等待,数据库问题 如何用TDateTime类型表示类似yyyy-mm-dd hh:mm:ss的格式 谁能告诉我一个不用认证的SMTP服务器? 求解一个报表的问题 sql 怎么去处重复数据 datagrid 简单问题(我是菜鸟)
p:PChar;
begin
p:='abc'+#0+'asdfds';
showmessage( string( p ) );
end;
2、数组的第一个元素的指针,是数组的指针
如有:
a : array [0..254] of char;
p,p0 : pcahr;p := @a;//数组的指针
p0 := @a[0];//指向数组的第一个元素的指针@a与@a[0]等价,p与p0代表了同一个东西
等待更一步的解释,
作者:小冬 (kend)
日期:2007-11-21
原文出处:http://myvnet.com/article.asp?id=13
http://myvnet.com/
http://www.cnpack.org/数组与字符串内容的互相转换往往采用Move或CopyMemory来进行,我总结了一下它们的规则:1.Move时如果访问的是string或string[n],要用string[1],因为string[0]是字符串的长度,所以所有string都以string[1]开始。
2.Move时如果访问的是array,静态array(array[0..n] of Char)时直接使用,动态array(array of Char)时使用array[0]。因为动态array时变量名其实只是一个指针地址,不代表数组内容的地址。
3.Move时如果访问的是PChar,要使用^取指向的内容。 Move时如果目标是数组的话:
1.静态数组array[0..n] of Char,取两者最小长度直接Move。
2.动态数组array of Char,取string长度并把数组SetLength后再Move 如果目标是string,无论是string或string[n]都要先SetLength。如果目标是PChar,要用GetMemory申请内存,使用完后可别忘了FreeMemory。(以上string指AnsiString,string[n]指ShortString)下面是简单的一点测试代码,我没有添加结果输出,大家可以自己用F8跟踪看结果:// 字符串到数组的转换
procedure StringToArray;
var
s1: string;
s2: string[20];
buf1: array[0..19] of Char;
buf2: array of Char; len1, len2: Integer; procedure cls;
begin
s1 := '';
s2 := '';
buf1 := '';
SetLength(buf2, 0);
SetLength(buf2, 20);
len1 := 0;
len2 := 0;
end;begin
// string to char array
cls;
s1 := 'http://MyvNet.com';
len1 := Length(s1);
len2 := Length(buf1);
Move(s1[1], buf1, Min(len1, len2)); // string to dynamic char array
cls;
s1 := 'http://MyvNet.com';
len1 := Length(s1);
len2 := Length(buf2);
Move(s1[1], buf2[0], Min(len1, len2)); // string array to char array
cls;
s2 := 'http://MyvNet.com';
len1 := Length(s2);
len2 := Length(buf1);
Move(s2[1], buf1, Min(len1, len2)); // string array to dynamic char array
cls;
s2 := 'http://MyvNet.com';
len1 := Length(s2);
len2 := Length(buf2);
Move(s2[1], buf2[0], Min(len1, len2)); cls;
end;// 数组到字符串的转换
procedure ArrayToString;
var
s1: string;
s2: string[20];
buf1: array[0..19] of Char; len1, len2: Integer; procedure cls;
begin
s1 := '';
s2 := '';
buf1 := '';
SetLength(buf2, 0);
SetLength(buf2, 20);
len1 := 0;
end;begin
// char array to string
cls;
buf1 := 'http://MyvNet.com';
len1 := Length(buf1);
SetLength(s1, len1);
Move(buf1, s1[1], len1); // char array to string array
cls;
buf1 := 'http://MyvNet.com';
len1 := Length(buf1);
SetLength(s2, len1);
Move(buf1, s2[1], len1);
// or
//Move(buf1, s2[1], len1);
//s2[0] := Chr(len1); // dynamic char array to string
cls;
buf1 := 'http://MyvNet.com';
Move(buf1, buf2[0], Length(buf1));
len1 := Length(buf2);
SetLength(s1, len1);
Move(buf2[0], s1[1], len1); // dynamic char array to string array
cls;
buf1 := 'http://MyvNet.com';
Move(buf1, buf2[0], Length(buf1));
len1 := Length(buf2);
SetLength(s2, len1);
Move(buf2[0], s2[1], len1);
// or
//Move(buf2[0], s2[1], len1);
//s2[0] := Chr(len1); cls;
end;// PChar和数组的转换
procedure TForm1.PCharAndArray;
var
pc: PChar;
buf1: array[0..19] of Char;
buf2: array of Char;
len1: Integer; procedure cls;
begin
pc := '';
buf1 := '';
SetLength(buf2, 0);
SetLength(buf2, 20);
len1 := 0;
end;begin
// PChar to array
cls;
pc := 'http://MyvNet.com';
len1 := Length(pc);
Move(pc^, buf1, len1); // PChar to dynamic array
cls;
pc := 'http://MyvNet.com';
len1 := Length(pc);
Move(pc^, buf2[0], len1); // array to PChar
cls;
buf1 := 'http://MyvNet.com';
len1 := Length(buf1);
pc := GetMemory(len1);
Move(buf1, pc^, len1);
FreeMemory(pc); // dynamic array to PChar
cls;
buf1 := 'http://MyvNet.com';
Move(buf1, buf2[0], Length(buf1));
len1 := Length(buf2);
pc := GetMemory(len1);
Move(buf2[0], pc^, len1);
FreeMemory(pc); cls;
end;