々
々飞翔鸟々
Unregistered / Unconfirmed
GUEST, unregistred user!
先给出delphi的例子:返回结果是正确的function a()Char;var s: PChar;begin s := 'abcdefghi'; result := s;end;procedure TForm1.Button1Click(Sender: TObject);var str: PChar;begin str := a(); Application.MessageBox(str,'te');end;下面给出C++的例子:#include<iostream>using namespace std;char *f();char *f1();int main(){ cout << f() << endl; cout << f1() << endl; return 0;}char * f(){ char s[] = "Hello"; return s;}char *f1(){ static char s[] = "World" ; return s;}第一个打印的是乱码,因为s是局部变量,离开f则释放空间第二个打印正确,因为s是静态变量,整个文件范围类都存在谁能解释一下为什么Delphi返回临时指针的结果是对的呢?