楼主:
sighAll (sigh)
2018-07-29 17:54:53大家好 朋友问了一个题目 说哪里有错
char * saveString(const char * s)
{
// allocate memory for a copy of the input string s
char *p = (char*) malloc(strlen(s));
while (*s)
{
*p++ = *s++;
}
*p = '\0'; // null-terminate saved copy
return p;
}
小弟研究半天 后来终于找到解答 多一个char *temp 去让p指过去 最后return temp,
但我不晓得为什么原本的写法有问题
以下是可以compiler和回传回来是正确的code
请大大开示 谢谢!!
char * saveString(char * s)
{
// allocate memory for a copy of the input string s
char *p = (char*) malloc(strlen(s));
char *temp;
temp = p;
while (*s)
{
printf("*s=%c\n", *s);
*p++ = *s++;
}
*p = '\0'; // null-terminate saved copy
return temp;
}