for(c=i;c >=0;c--){
*l++ = s[c]; // you are assigning `\0` for first time
}
你返回 return l; 并且在 l\0 的第一个位置是这样
所以在带有 %s
的 printf 语句中
printf("\n %s \n",str_rev(a));
什么都不打印。
建议:
for(c=i-1;c >=0;c--){
// ^ c should be i-1 initially
*l++ = s[c];
}
(2):您至少在问题代码中有两个编译错误。你忘记了 ; 两个位置
return "INVALID PARAMS";
^
下一步
char a[]="Angus Declan R";
^
第三个严重错误
您正在返回无效的内存地址!
你在做什么,在 st 分配内存,然后分配给 l,然后 free(st) 并返回 l:(阅读评论)
st = malloc((strlen(s) * sizeof(*s))+1); // allocation
l = st; // assign to l
// code in between
free(st); // st and l become invalid after free
return l; // returning invalid memory
建议您是否使用 l 并在不调用 free() 的情况下返回 st。
(4):
这不是错误,但为什么会出现这个无用的循环?
while(c > 0){
l--;
c--;
}
(5): 忘记了 l
前面的 *
for(c=i;c >=0;c--){
*l++ = s[c];
}
l = '\0';
^ forgot * it should be *l = '\0';