【发布时间】:2021-01-21 02:25:40
【问题描述】:
所以我有这个函数应该反转一个字符串然后返回它
char *my_revstr(char *str)
{
int i = 0;
int j = 0;
int k = 0;
while(str[j] != '\0') {
j++;
}
j--;
while(i < j) {
k = str[i];
str[i] = str[j];
str[j] = k;
i++;
j--;
}
return (str);
}
但是每当我尝试运行它时,我都会遇到分段错误,老实说,我不太清楚为什么。我希望有人能帮我解决这个问题^^。
【问题讨论】:
-
代码对我来说很好,你试过调试吗?它还会在每个测试用例中始终崩溃吗?
-
请显示minimal reproducible example。你打电话给
my_revstr的方式可能是问题所在。my_revstr函数本身在我看来是正确的。 -
va voir tes AERs plutot ;p
标签: c reverse c-strings string-literals function-definition