问题描述
使用字符串操作函数指定 strcpy
我做了这个小程序。
While using string manipulation functions specificaly strcpy
I did this small program.
char s1[8]="Hellopo";
char s2[4]="sup";
strcpy(s1,s2);
cout<<s1<<endl;
当我打印出s1它实际上只是打印出sup。我希望它打印suplopo。
When I printed out s1 It actually just printed out "sup". I expected it to print "suplopo".
然后我这样做:
cout<<< s1 + 4< endl;
它打印出opo;
这个: cout 没有什么
它。
我得出这个结论。因为C ++在到达null终止符时停止输出该字符串。因此,必须在 strcpy
函数中复制null。产生此字符串:
I came to this conclusion. Since C++ stops outputing the string once it reaches the null terminator. Therefore the null must have been copied in the strcpy
function. Resulting in this string:
s - u - p - \0 - o - p - o -\0;
s - u - p - \0 - o - p - o -\0;
请告诉我这是否正确。
Please tell me if this is correct or not. And if im not please correct me.
如果您有任何更多的信息可以提供。
And if you have any more info to provide please do.
推荐答案
您的推理是正确的,很容易被任何体面的手册:
Your reasoning is correct, and would have easily been confirmed by any decent manual:
这篇关于C ++:strcpy函数复制null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!