C ++:strcpy函数复制null? [英] C++: strcpy Function copies null?

查看:750
本文介绍了C ++:strcpy函数复制null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用字符串操作函数指定 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:


strcpy()函数复制'\0')的c $ c> src > dest 。

The strcpy() function copies the string pointed to by src, including the terminating null byte ('\0'), to the buffer pointed to by dest.

这篇关于C ++:strcpy函数复制null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆