#include<iostream>
using namespace std;
char *getMemory(){
char p []="hello";
return p;
}
int main(){
char *str=NULL;
str=getMemory();
cout<<str<<endl;
return 0;
}
结果输出可能乱码,也可能是hello
分析:数组p是局部变量,存在于栈内存,返回时作用域结束,内容已被清除。str指针虽然指向不为空,但是指向的内容已被清除,输出结果不确定。