请看看这个问题,谢谢!(10分)

A

awfigsk

Unregistered / Unconfirmed
GUEST, unregistred user!
func function ShortStringAsPChar(var S:ShortString):pChar;
{这函数能使一个字符串以null结尾,这样就能传递给需要PChar类型参数的Win32 API函数,如果字符串超过
254个字符,多出的部分将被截掉}
begin
if Length(S)=High(S) then Dec(S[0])
{ 如果S太长,就截取一部分}
S[Ord(Length(S))+1]:=#0
{ 把null加到字符串的最后}
Result:=@S[1]
{ 返回PChar化的字符串}
end;
我想请问:在函数部分s[ord(length(s))+1]:=#0;这条语句中为什么不直接用length(s)
返回字符串s的长度,而在外面还要加上ord()函数?这有什么区别吗?
 
[blue]function Ord(X);
Description
X is an ordinal-type expression. The result is the ordinal position of X
its type is the smallest standard integer type that can hold all values of X's type.
Ord cannot operate on Int64 values.[/blue]

所以这一段
[blue]S[Ord(Length(S))+1]:=#0;[/blue]才是{ 如果S太长,就截取一部分}并且{ 把null加到字符串的最后}吧?

[red]瞎猜得[/red][:D][:D][:D]


 
用s[length(s)+1]:=#0这条语句难道不能在字符串最后加上NULL字符吗?
 
我得意思是说[red]S[Ord(Length(S))+1]:=#0[/red]里Ord大概能保证返回的值是一个短整型,
因为[blue]The result is the ordinal position of X
its type is the smallest standard
integer type that can hold all values of X's type.[/blue]

[red]瞎猜得[/red][:D][:D][:D]
 
数组s[..]中括号里面的数要是短整型的数吗?
 
直接用length(s)就可以了
如果写成这样:
if Length(S)=High(S) then Dec(S[0])
{ 如果S太长,就截取一部分}
Len := Length(S);
OLen := Ord(len);
S[OLen+1]:=#0
{ 把null加到字符串的最后}
Result:=@S[1]
{ 返回PChar化的字符串}
并打开编译优化选项
OLen := Ord(len);
这句将被忽略,所以不是必需的

 
估计这段代码是Delphi1或者Delphi2的吧
 
谢谢二位的指点!
 
顶部