#include <iostream>
#include <cstring>
using namespace std;
char*getname(void);
int main()
{
char*name;
name=getname();
cout<<name<<" at "<<(int*)name<<endl;
delete [] name;
name=getname();
cout<<name<<" at "<<(int*)name<<endl;
delete [] name;
return 0;
}
char*getname()
{
char temp[80];
cout<<"Enter last name:";
cin>>temp;
char*pn=new char[strlen(temp)+1];
strcpy(pn,temp);
return pn;
}
这是编译结果
Enter last name:a
a at 00380930
Enter last name:b
b at 00381D68
Press any key to continue
pn指针指向的地址应该只有cin>>temp的字符串吧,比如只有一个a。怎么连上面那句"Enter last name:";都有了。
江户川乱折腾
青春有我
慕哥9229398
相关分类