可能这是个棘手的问题,但请帮忙
void Temp1::caller()
{
char *cc=Called();
printf("sdfasfasfas");
printf("%s",cc);
}
char *Temp1::Called()
{
char a[6]="Hello";
return &a;
}
这里如何使用 printf("%s",cc); 打印 Hello
最佳答案
首先,这个函数:
char *Temp1::Called()
{
char a[6]="Hello";
return &a;
}
正在返回一个局部变量,一旦函数结束,该变量将不复存在 - 更改为:
const char *Temp1::Called()
{
return "Hello";
}
然后,使用 printf() 打印字符串的方法是使用“%s”:
void Temp1::caller()
{
const char *cc=Called();
printf("sdfasfasfas");
printf("%s",cc);
}
关于c++ - 将 char[] 转换为 char*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2666300/