DELPHI巨搞笑问题!!!!!!!!!!!!!!! (100分)

  • 主题发起人 主题发起人 mphst
  • 开始时间 开始时间
M

mphst

Unregistered / Unconfirmed
GUEST, unregistred user!
{
Function MyTran(pstr:PCHAR):PCHAR
being
...
end;}


{main}
str:string;
...
str:=MyTran((PCHAR(str));
...

其中在MyTran函数中跟踪返回值正常,返回后常常会在str后面多加一个字符,有誰碰到类似问题没有?

 
没有,你这个函数有什么意义啊???
 
真是好笑.
str为string类型,却赋值为pchar,要赋值也应该
str:=strpas( MyTran((PCHAR(str)))

才行.
 
真的很好笑[:D][:D][:D]
 
Function MyTran(pstr:PCHAR):PCHAR;
begin
Result:=pstr;
end;

procedure TForm1.Button1Click(Sender: TObject);
const
s='ABC';
var
str:string;
begin
ShowMessage(StrPas(MyTran(PChar(s))));
Str:=StrPas(MyTran(PChar(Str)));//和下面的一行分别试
Str:=MyTran(PChar(Str));
ShowMessage(Str);
end;

//没有问题的, 我用上面的三种试的, 没有添加字符

//另:string 和 C的字符串不一样的, PChar的一样都在后面添加一空字符
 
类型不对了,一个STRING,一个PCHAR,要转换嘛
StrPas(Str: PChar): string;
 
string 和 pchar是不一样的,前者没有结束标志,由首地址表示长度;
后者有'#0'结束标志
 
巨搞笑[:D][:D][:D]
 
str:string;
...
str:=MyTran((PCHAR(str+ #0 ));
~~~~~
 
补充,以下是delphi官方网站上的提示:
As mentioned earlier, AnsiString types are always null-terminated, so they’re compatible with
null-terminated strings. This makes it easy to call Win32 API functions or other functions
requiring PChar-type strings. All that’s required is that you typecast the string as a PChar.
(Typecasting is explained in more detail in the section “Typecasting and Type Conversion.”)
The following code demonstrates how to call the Win32 GetWindowsDirectory() function,
which accepts a PChar and buffer length as parameters:
var
S: string;
begin
SetLength(S, 256)
// important! get space for string first
// call function, S now holds directory string
GetWindowsDirectory(PChar(S), 256);
end;
After using an AnsiString in which a function or procedure expects a PChar, you must manually
set the length of the string variable to its null-terminated length. The RealizeLength()
function, which also comes from the StrUtils unit, accomplishes that task:
procedure RealizeLength(var S: string);
begin
SetLength(S, StrLen(PChar(S)));
end;
Calling RealizeLength() completes the substitution of a long string for a PChar:
var
S: string;
begin
SetLength(S, 256)
// important! get space for string first
// call function, S now holds directory string
GetWindowsDirectory(PChar(S), 256);
RealizeLength(S)
// set S length to null length
end;
 
我却不知你们笑什么?
 
谢谢各位,接受答案了,来者有分
 

Similar threads

S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
900
SUNSTONE的Delphi笔记
S
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
后退
顶部