64,648
社区成员
发帖
与我相关
我的任务
分享
只能这样解释:
char * pcStr = "abc"; // "abc" 编译时常量区分配
string str = "abc"; // "abc" 运行时栈上分配
std::string a="abc";
pTest=(char*)a.c_str();
注释掉,把下面本来的注释去掉就能显示正常,请问为什么
char* test()
{
std::string a="abc";
pTest=(char*)a.c_str();
char*p="abc";
pTest=p;
return pTest;
}
char*pTest=NULL;
char* test()
{
std::string a="abc";
pTest=a.c_str();
cout<<"pTest1 = "<<pTest<<endl;
return pTest;
}
int main()
{
test();
cout<<"1"<<endl;
if(pTest==NULL)
cout<<"2"<<endl;
assert(pTest!=NULL);
cout<<"pTest = "<<pTest<<endl;
}
$ SS
pTest1 = abc
1
pTest =
char * cp1 = "abc";
char * cp2 = "abc";
char cp3[] = "abc";
char cp4[] = "abc";
cout << (void *) cp1 << endl;
cout << (void *) cp2 << endl;
cout << (void *) cp3 << endl;
cout << (void *) cp4 << endl;