strncpy 和 memcpy 之间的区别?
如何访问s
中的s[7]
?
我没有观察到 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;
}
输出:
qwerty
qwerty
qwerty
How can I access s[7]
in s
?
I didn't observe any difference between strncpy
and 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:
#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;
}
Output:
qwerty
qwerty
qwerty
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
其他人指出了您的空终止问题。在了解
memcpy
和strncpy
之间的区别之前,您需要先了解空终止。主要区别在于,
memcpy
将复制您要求的所有 N 个字符,而strncpy
将最多复制第一个空终止符(包括第一个空终止符)或 N 个字符(以其中一个为准)更少。如果它复制的字符少于 N 个字符,它将用空字符填充其余部分。
Others have pointed out your null-termination problems. You need to understand null-termination before you understand the difference between
memcpy
andstrncpy
.The main difference is that
memcpy
will copy all N characters you ask for, whilestrncpy
will copy up to the first null terminator inclusive, or N characters, whichever is fewer.In the event that it copies less than N characters, it will pad the rest out with null characters.
您得到输出
querty
是因为索引7
不正确(数组从 0 开始索引,而不是 1)。索引6
处有一个 空终止符 来表示字符串,以及它后面的任何内容都不会产生任何效果。您需要修复两件事:
s[7]
中的7
更改为6
s[7] 处添加空终止符]
结果将是:
原始不起作用和固定工作。
至于
strncpy
与memcpy
,区别在于strncpy< /code> 为您添加一个空终止符。但是,仅当源字符串在
n
之前有一个时。所以strncpy
就是你想要在这里使用的,但要非常小心大的 BUT。You are getting the output
querty
because the index7
is incorrect (arrays are indexed from 0, not 1). There is a null-terminator at index6
to signal the end of the string, and whatever comes after it will have no effect.Two things you need to fix:
7
ins[7]
to6
s[7]
The result will be:
Original not working and fixed working.
As for the question of
strncpy
versusmemcpy
, the difference is thatstrncpy
adds a null-terminator for you. BUT, only if the source string has one beforen
. Sostrncpy
is what you want to use here, but be very careful of the big BUT.即使您指定了要复制的字节数,Strncpy 也将复制最多
NULL
,但 memcpy 最多会复制指定的字节数。printf 语句将打印最多 NULL ,因此您将尝试打印单个字符,它将显示 ,
OUTPUT
Strncpy will copy up to
NULL
even you specified the number of bytes to copy , but memcpy will copy up to specified number of bytes .printf statement will print up to NULL , so you will try to print a single charater , it will show ,
OUTPUT
要制作“qwertyA”,您需要设置
s[6] = 'A'
和s[7]='\0'
字符串从 0 开始索引,因此
s[0] == 'q'
,并且它们需要以 null 终止。To make "qwertyA" you need to set
s[6] = 'A'
ands[7]='\0'
Strings are indexed from 0, so
s[0] == 'q'
, and they need to be null terminated.当你有:
这就是该数组包含的内容:
如果你想在字符串末尾添加一个“A”,那就是索引 6,因为数组索引从 0 开始
请注意,当你以这种方式初始化数组时,剩余的space 设置为 0(nul 终止符),虽然在这种情况下不需要,但通常要注意您需要使字符串以 nul 终止。
例如,
在上面的示例中,“qwerty”(包括其 nul 终止符)被复制到
s
。 s[6] 覆盖 nul 终止符。由于s
的其余部分尚未初始化,我们需要自己添加一个 nul 终止符s[7] = 0;
When you have:
this is what that array contains:
If you want to add an 'A' to the end of your string, that's at index 6, since array indexes start at 0
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.
In the above example "qwerty" , including its nul terminator is copied to
s
. s[6] overwrites that nul terminator. Since the rest ofs
is not initialized we need to add a nul terminator ourselves withs[7] = 0;
正如 Philip Potter 所解释的,主要区别在于 memcpy 将复制您要求的所有 n 个字符,而 strncpy 将复制最多包含第一个空终止符的内容,或 n 个字符,以较少者为准。如果 strncpy 复制的字符少于 N 个字符,它将用空字符填充其余部分。
下面的程序将回答你的问题:
o/p:
执行下面的代码并检查差异,你可能会发现它很有用。
输出:
As explained by Philip Potter, the main difference is that memcpy will copy all n characters you ask for, while strncpy will copy up to the first null terminator inclusive, or n characters, whichever is less. In the event that strncpy copies less than N characters, it will pad the rest out with null characters.
The below program will answer your question:
o/p:
Execute the below code and check the difference, you might find it useful.
Output: