new:
© / Posted in 知识如海 / September 8, 2009
如下两个函数,都是返回一个局部变量,但是在main函数里打印的时候一个打印出“hello world”,一个打印出乱码,为什么? #include <iostream.h>
char *GetMemory1()
{
char *p="hello world";
return p;
}
char *GetMemory2()
{
char p[]="hello world";
return p;
}
void main()
{
char *str1 = NULL;
char *str2 = NULL;
str1 = GetMemory1();
str2 = GetMemory2();
cout<<str1<<endl<<str2<<endl;
}
- 分页: