C++:strcpy 函数复制 null?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11830979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++: strcpy Function copies null?
提问by Mohamed Ahmed Nabil
While using string manipulation functions specificaly strcpy
I did this small program.
在专门使用字符串操作函数时,strcpy
我做了这个小程序。
char s1[8]="Hellopo";
char s2[4]="sup";
strcpy(s1,s2);
cout<<s1<<endl;
When I printed out s1 It actually just printed out "sup". I expected it to print "suplopo".
当我打印出 s1 它实际上只是打印出“sup”。我希望它打印“suplopo”。
Then I did this:
然后我这样做了:
cout<<s1+4 << endl;
cout<<s1+4 << endl;
It printed out "opo";
它打印出“opo”;
And The output of this: cout<<s1+3<<endl;
was nothing
而这个输出:cout<<s1+3<<endl;
什么都没有
So after thinking a bit about it.
所以想了想之后。
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:
我得出了这个结论。由于 C++ 一旦到达空终止符就停止输出字符串。因此,必须在strcpy
函数中复制了 null 。导致此字符串:
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.
如果您有更多信息要提供,请提供。
回答by Kerrek SB
Your reasoning is correct, and would have easily been confirmed by any decent manual:
您的推理是正确的,并且很容易被任何体面的手册证实:
The
strcpy()
function copies the string pointed to bysrc
, including the terminating null byte ('\0'
), to the buffer pointed to bydest
.
该
strcpy()
函数将 指向的字符串src
(包括终止空字节 ('\0'
) )复制到 指向的缓冲区中dest
。
回答by paxdiablo
Your reasoning regarding the copying of the terminating character is correct. The C++ standard (which is the definitive specification for the language) defers to C on this matter (for example, C++14 defers to C99, and C++17 defers to C11).
您关于复制终止字符的推理是正确的。C++ 标准(它是该语言的权威规范)在这方面遵循 C(例如,C++14 遵循 C99,而 C++17 遵循 C11)。
The C11 standard has this to say about strcpy
:
C11 标准是这样说的strcpy
:
7.24.2.3 The
strcpy
functionSynopsis:
#include <string.h>
char *strcpy(char * restrict s1, const char * restrict s2);
Description:
The
strcpy
function copies the string pointed to bys2
(including the terminating null character) into the array pointed to bys1
. If copying takes place between objects that overlap, the behavior is undefined.Returns:
The
strcpy
function returns the value ofs1
.
7.24.2.3
strcpy
功能概要:
#include <string.h>
char *strcpy(char * restrict s1, const char * restrict s2);
描述:
该
strcpy
函数将 指向的字符串s2
(包括终止空字符)复制到 指向的数组中s1
。如果复制发生在重叠的对象之间,则行为未定义。返回:
该
strcpy
函数返回 的值s1
。
If you just wanted to replace the first three characters of your string, you can use memcpy()
to copy a specific number of bytes:
如果您只想替换字符串的前三个字符,您可以使用memcpy()
复制特定数量的字节:
memcpy(s1, s2, strlen(s2));
Keep in mind that this will just copy those bytes and nothing more. If s1
isn't already a string of at least the length of s2
, it's unlikely to end well :-)
请记住,这只会复制这些字节,仅此而已。如果s1
不是至少长度为 的字符串s2
,则不太可能结束:-)
And just keep one thing in mind re your comment "... resulting in this string: sup\0opo\0".
请记住一件事,即您的评论“...导致此字符串:sup\0opo\0”。
That is nota string. A string in C (and a legacy string in C++) is defined as a series of characters up to and including the first\0
terminator.
那不是字符串。C 中的字符串(以及 C++ 中的旧字符串)被定义为直到并包括第一个\0
终止符的一系列字符。
You may well have a series of characters up to the original (now second) \0
but the stringis actually shorter than that. This may seema little pedantic but it's important to understand the definitions.
您可能有一系列字符直到原始字符(现在是第二个字符),\0
但字符串实际上比原来的要短。这可能看起来有点迂腐,但理解定义很重要。
回答by steveg89
You are correct. For the effect you initially expected, you would use strncopy
. strncopy
copies the null terminator as long as you specify the correct length of the string that is being copied.
你是对的。对于您最初预期的效果,您将使用strncopy
. strncopy
只要指定要复制的字符串的正确长度,就会复制空终止符。
回答by Dave Rager
This is correct.
这是对的。
http://pubs.opengroup.org/onlinepubs/009695399/functions/strcpy.html
http://pubs.opengroup.org/onlinepubs/009695399/functions/strcpy.html
The strcpy() function shall copy the string pointed to by s2 (including the terminating null byte)into the array pointed to by s1.
strcpy() 函数应将 s2 指向的字符串(包括终止空字节)复制 到 s1 指向的数组中。
回答by sradforth
Yes, this is correct. strcpy will include the null terminator. It's important as if you copy the string to a new memory block you want it null terminated by default. I believe strncpy might be what you're after in this case.
是的,这是正确的。strcpy 将包含空终止符。这很重要,就像您将字符串复制到新的内存块一样,您希望它默认为空终止。我相信 strncpy 可能是你在这种情况下所追求的。
Also I know it was only testing code but I'd be careful in this day and age of using +X offsets in strings... ascii normally bites people in the rear end in the utf8/unicode world we now live in.
我也知道它只是测试代码,但在这个时代,我会小心在字符串中使用 +X 偏移量......在我们现在生活的 utf8/unicode 世界中,ascii 通常会在后端咬人。
回答by xbonez
From the man page for strcpy
:
从手册页strcpy
:
The
strcpy()
function copies the string pointed to bysrc
, including the terminating null byte ('\0'
), to the buffer pointed to bydest
. The strings may not overlap, and the destination stringdest
must be large enough to receive the copy.
该
strcpy()
函数将 指向的字符串src
(包括终止空字节 ('\0'
) )复制到 指向的缓冲区中dest
。字符串不能重叠,并且目标字符串dest
必须足够大以接收副本。
回答by NovicE_Neo
Yes, you are correct in your reasoning and if you would have explicitly typecast-ed the 4th character 's1[3]' as integer like this:
是的,您的推理是正确的,如果您将第 4 个字符 's1[3]' 显式转换为整数,如下所示:
cout<<(int)s1[3];
cout<<(int)s1[3];
You would have gotten "0" as the output which is the ASCII value of NULL character.
你会得到“0”作为输出,它是 NULL 字符的 ASCII 值。