char * getString(void)
{
char p[] = "hello world";
return p;
}
//p 中保存的是栈中内容hello world 的首地址,而随着函数调用的结束hello world 被清除,所以p中保存的是乱码,函数的返回值正是指向该乱码的指针
void test4(void)
{
char *str = NULL;
str = getString1();
cout << str << endl;
}
char * getString2(void)
{
char *p = "hello world";
return p;
}
//p中保存的是常量区hello world 的首地址,函数调用结束时hello world 不会被清除,所以返回的仍然是常量区中hello world 的首地址
void test5(void)
{
char *str = NULL;
str = getString2();
cout << str << endl;
}
{
char p[] = "hello world";
return p;
}
//p 中保存的是栈中内容hello world 的首地址,而随着函数调用的结束hello world 被清除,所以p中保存的是乱码,函数的返回值正是指向该乱码的指针
void test4(void)
{
char *str = NULL;
str = getString1();
cout << str << endl;
}
char * getString2(void)
{
char *p = "hello world";
return p;
}
//p中保存的是常量区hello world 的首地址,函数调用结束时hello world 不会被清除,所以返回的仍然是常量区中hello world 的首地址
void test5(void)
{
char *str = NULL;
str = getString2();
cout << str << endl;
}