【发布时间】:2019-09-14 21:26:39
【问题描述】:
这会引发错误 id 返回 1 错误状态。循环正在工作,但我无法打印最终的连接字符串。当我尝试打印最终的字符串时,它什么也不做。
char str1[50], str2[50], str[100];
char *p1, *p2, *p3;
int i = 0, j = 0;
p1 = &str1[0];
p2 = &str2[0];
p3 = &str[0];
printf("enter a string:");
gets(str1);
printf("enter a string:");
gets(str2);
while (i <= strlen(str1) + strlen(str2)) {
if (i != strlen(str1)) {
*(p3 + i) = *(p1 + i);
i++;
} else {
*(p3 + i) = *(p2 + j);
j++;
}
【问题讨论】:
-
欢迎来到 Stack Overflow! Why is the gets function so dangerous that it should not be used?
-
“但我无法打印最终的连接字符串” - 为什么不显示打印的代码?另外,您是否正确添加了 NUL 终止符?
-
你到底在哪里打印?
-
欢迎来到 Stack Overflow!与其描述你的代码不起作用,你能展示一下吗?
-
欢迎来到 Stack Overflow!对于这个问题,我们需要看到minimal reproducible example。另外:idownvotedbecau.se/nomcve
标签: c string loops pointers strlen