在编程的时候偶尔会遇到一个字符串的问题,好像是这样说:不能把 const char* 转换成 TCHAR * ,只是这个错误有时候有,有时候没有,也没有深入关注过,只知道 "abc" 应该是一个const 型的。
今天偶然看到2个帖子,终于对这个问题有了比较清晰的理解
贴一:
http://topic.youkuaiyun.com/u/20090302/17/900b3797-3642-4569-a623-dc0f8ebd8401.html?seed=1325371970
#include <stdio.h>
int A()
{
int test=10;
return test;
}
int main()
{
int a=A();
printf("%d/n",a);
return 0;
}
上面的代码能编译通过 我想问 在A() 函数中的 test 变量的生存期不是只在A()函数体内吗? 怎么还能成功返回呢
下面的这段代码为何就不行呢 两个程序中的变量生存期有什么区别啊?
#include <stdio.h>
char* A()
{
char p[]="hello world";
return p;
}
int main()
{
char *str=NULL;
str=A();
printf("%s",str);
}
比较好的答案是:
一: