【发布时间】:2013-02-25 05:15:06
【问题描述】:
#include<stdio.h>
#include<string.h>
#include<malloc.h>
char *str_rev(char s[]){
static char *st = NULL;
static char *l;
int i = 0,c = 0;
st = malloc((strlen(s) * sizeof(*s))+1);
*l = st;
if(s == NULL){
return "INVALID PARAMS";
}
for(i=0;s[i]!='\0';i++){
;
}
for(c=i;c >=0;c--){
*l++ = s[c];
}
l = '\0';
return st;
}
int main(){
char a[]="Angus Declan R";
printf("\n %s \n",str_rev(a));
return 0;
}
如何释放在 func str_rev() 中使用 malloc() 分配的内存,因为我需要重新运行反转的字符串。
【问题讨论】:
-
这是你的真实代码吗?它无法编译。
-
请用问题陈述和几句话解释您的解决方法来改进您的问题。
-
@nm :是的,这是我的真实代码。它正在编译,但字符串没有以相反的顺序打印。
-
你用的是什么编译器?代码不是有效的 C,它在两个地方缺少分号。
标签: c