const char* returnStr()
{
char time[40] = {"France"};
char* time1;
time1 = time;
return time1;
}
int main(int argc, char* argv[]) {
printf ("return String is %s\n",returnStr());
}
此代码返回一些垃圾字符。 const char*
是否不足以返回本地 char 指针?我是否也必须在函数中使用静态?
最佳答案
Do I have to use the static too in the function?
是的。 const
只是返回值的限定符,向 returnStr
的调用者发出信号,告知他们不应修改函数的结果。它不会改变 time
的临时字符。
关于c - 在 C 中返回本地指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8220401/