Ran*_*vas 8 c arrays string null copying
我想将字符串中的元素转移到另一个字符串,因此编写了以下程序.最初,我认为for循环应该执行,直到复制了NULL字符(包括它).但是在这段代码中,如果找到一个NULL字符(即尚未复制),for循环终止,但它仍然能够显示已复制元素的字符串.如果首先没有NULL字符,这怎么可能?
#include<stdio.h>
#include<stdlib.h>
int main()
{
char temp[100], str[100];
fgets(str, 100, stdin);
int i;
for(i = 0; str[i]!='\0'; i++)
{
temp[i] = str[i];
}
puts(temp);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是我的计算机上的输入和输出:
0
0
??`
Process returned 0 (0x0) execution time : 1.863 s
Press any key to continue.
Run Code Online (Sandbox Code Playgroud)
看垃圾"絯忐`"?这是未定义的行为.你的计划运作良好,因为你(非)幸运.
同样,不确定的行为,不值得太多的讨论.
| 归档时间: |
|
| 查看次数: |
1517 次 |
| 最近记录: |