sle*_*743 1 c arrays string reverse pointers
#include <stdio.h>
void reverse(int len, char s[], char b[]);
int main() {
char s[5] = "hello";
char b[5];
reverse(5, s, b);
return 0;
}
void reverse(int len, char s[], char b[]) {
int i;
for (i = 0; i < len; i++) {
b[(len - 1) - i] = s[i];
}
printf("%s : %s\n", s, b);
printf("%s", b);
}
Run Code Online (Sandbox Code Playgroud)
这是我上面的代码C.当我运行它时,它将字符串切换s[]为b[]但b[]也包括s[].有人可以请彻底解释我做错了什么吗?对此,我真的非常感激.
char s[5] = "hello";
Run Code Online (Sandbox Code Playgroud)
这是错误的,你需要一个[6]以便存储尾随'\0'(相同b)
最好省略数组大小:
char s[] = "hello";
char b[sizeof(s)];
Run Code Online (Sandbox Code Playgroud)
你需要b[i] = '\0';在for循环之后.
| 归档时间: |
|
| 查看次数: |
162 次 |
| 最近记录: |