【发布时间】:2026-01-20 13:25:01
【问题描述】:
我想知道这里发生了什么,我在 reverse 函数之外为 dest 变量分配空间。然后在函数调用中写入它。但是我的输出是空白的。我想在没有 malloc 的情况下这样做,纯粹是出于教育目的。谢谢
#include <stdio.h>
#include <string.h>
/*function declaration*/
void reverse(char *src, char *dest);
int main(void) {
char src[] = "hello, world";
char dest[strlen(src)];
reverse(src, dest);
printf("%s\n", dest);
return 0;
}
/*function reverse a string*/
void reverse(char *src, char *dest) {
int i;
int j = 0;
for(i = strlen(src); i >= 0; i--) {
*(dest+j) = *(src+i);
++j;
}
}
【问题讨论】:
-
你知道你可以
dest[j] = src[i]吗?
标签: c arrays string pointers reverse