这是我的代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char f[] = "First";
char s[] = "Second";
char *tmp = malloc(strlen(f) + strlen(s) + 2);
strcpy(tmp, f);
strcpy(tmp, s);
printf("%s", tmp);
free(tmp);
return 0;
}
我正在尝试连接 f 和 s。问题是 tmp 仅包含“Second”作为数组。 我在这里想念的是什么
最佳答案
strcpy
将字符串复制到目标的开头,您需要 strcat
代替。
关于使用 strcpy 连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8821343/