在C中使用strcat()函数

用户

将C与A串联时,结果不是我想要的。如何获取hellojames而不是helloworldjames

char  A[12] =  "hello";
char  B[12] =  "world";
char  C[12] =  "james";

strcat(A,B);
printf("%s", A);
output = helloworld


strcat(A,C);
printf("%s",A);
output = helloworldjames
博士

当您执行strcat(A,B)时,您正在修改字符串A指向。您需要创建一个工作缓冲区。就像是:

char work[12];
strcpy(work, A);
strcat(work, B);
printf("%s", work);
strcpy(work, A);
strcat(work, C);
printf("%s", work);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章