C语言 strncpy 和 memcpy 的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4593907/
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
Difference between strncpy and memcpy?
提问by user559208
How can i access s[7]in s?
我如何才能获得s[7]的s?
I didn't observe any difference between strncpyand memcpy. If I want to print the output s, along with s[7](like qwertyA), what are the changes I have to made in the following code:
我没有观察到strncpy和之间的任何区别memcpy。如果我想打印输出s以及s[7](如qwertyA),我必须在以下代码中进行哪些更改:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char s[10] = "qwerty", str[10], str1[10];
s[7] = 'A';
printf("%s\n",s);
strncpy(str,s,8);
printf("%s\n",str);
memcpy(str1,s,8);
printf("%s\n",str1);
return 0;
}
/*
O/P
qwerty
qwerty
qwerty
*/
回答by Philip Potter
Others have pointed out your null-termination problems. You need to understand null-termination before you understand the difference between memcpyand strncpy.
其他人已经指出了您的空终止问题。你需要了解空终止你明白之间的差别之前memcpy和strncpy。
The difference is that memcpywill copy all N characters you ask for, while strncpywill copy up to the first null terminator inclusive, or N characters, whichever is fewer. (If it copies less than N characters, it will pad the rest out with null characters.)
不同之处在于memcpy将复制您要求的所有 N 个字符,而strncpy将复制到第一个空终止符或 N 个字符,以较少者为准。(如果它复制的字符少于 N 个,它将用空字符填充其余的字符。)
回答by moinudin
You are getting the output quertybecause the index 7is incorrect (arrays are indexed from 0, not 1). There is a null-terminatorat index 6to signal the end of the string, and whatever comes after it will have no effect.
您之所以得到输出,querty是因为索引7不正确(数组从 0 开始索引,而不是从 1 开始)。index 处有一个空终止符6来表示字符串的结束,它后面的任何内容都无效。
Two things you need to fix:
你需要解决的两件事:
- Change the
7ins[7]to6 - Add a null-terminator at
s[7]
- 更改
7在s[7]以6 - 在处添加一个空终止符
s[7]
The result will be:
结果将是:
char s[10] = "qwerty";
s[6] = 'A';
s[7] = 0;
Original not workingand fixed working.
As for the question of strncpyversus memcpy, the difference is that strncpyadds a null-terminator for you. BUT, only if the source string has one before n. So strncpyis what you want to use here, but be very careful of the big BUT.
至于strncpyvs的问题memcpy,区别在于strncpy为您添加了一个空终止符。但是,仅当源字符串在n. 这strncpy就是你想在这里使用的东西,但要非常小心大 BUT。
回答by GOBI
Strncpy will copy up to NULLeven you specified the number of bytes to copy , but memcpy will copy up to specified number of bytes .
NULL即使您指定要复制的字节数,Strncpy 也将复制,但 memcpy 将复制至指定的字节数。
printf statement will print up to NULL , so you will try to print a single charater , it will show ,
printf 语句最多打印 NULL ,因此您将尝试打印单个字符,它将显示,
printf("\t%c %c %c\t",s[7],str[7],str1[7]);
OUTPUT
输出
7 7
回答by Matt Ellen
To make "qwertyA" you need to set s[6] = 'A'and s[7]='\0'
要制作“qwertyA”,您需要设置s[6] = 'A'和s[7]='\0'
Strings are indexed from 0, so s[0] == 'q', and they need to be null terminated.
字符串从 0 开始索引,所以s[0] == 'q',它们需要以空字符结尾。
回答by nos
When you have:
当你有:
char s[10] = "qwerty";
this is what that array contains:
这是该数组包含的内容:
s[0] 'q' s[1] 'w' s[2] 'e' s[3] 'r' s[4] 't' s[5] 'y' s[6] 0 s[7] 0 s[8] 0 s[9] 0
If you want to add an 'A' to the end of your string, that's at index 6, since array indexes start at 0
如果你想在你的字符串末尾添加一个 'A',那就是在索引 6 处,因为数组索引从 0 开始
s[6] = 'A';
Note that when you initialize an array this way, the remaining space is set to 0 (a nul terminator), While not needed in this case, generally be aware that you need to make your strings nul terminated. e.g.
请注意,当您以这种方式初始化数组时,剩余空间将设置为 0(一个 nul 终止符),虽然在这种情况下不需要,但通常要注意您需要使您的字符串以 nul 终止。例如
char s[10];
strcpy(s,"qwerty");
s[6] = 'A';
s[7] = 0;
In the above example "qwerty" , including its nul terminator is copied to s. s[6] overwrites that nul terminator. Since the rest of sis not initialized we need to add a nul terminator ourselves with s[7] = 0;
在上面的例子 "qwerty" 中,包括它的 nul 终止符被复制到s. s[6] 覆盖了那个 nul 终止符。由于其余的s未初始化,我们需要自己添加一个 nul 终止符s[7] = 0;

