strncpy 和 memcpy 之间的区别?

发布于 2024-10-10 03:37:29 字数 542 浏览 0 评论 0原文

如何访问s中的s[7]

我没有观察到 strncpymemcpy 之间有任何区别。如果我想打印输出 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

怀中猫帐中妖 2024-10-17 03:37:29

其他人指出了您的空终止问题。在了解 memcpystrncpy 之间的区别之前,您需要先了解空终止。

主要区别在于,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 and strncpy.

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 fewer.

In the event that it copies less than N characters, it will pad the rest out with null characters.

享受孤独 2024-10-17 03:37:29

您得到输出 querty 是因为索引 7 不正确(数组从 0 开始索引,而不是 1)。索引 6 处有一个 空终止符 来表示字符串,以及它后面的任何内容都不会产生任何效果。

您需要修复两件事:

  1. s[7] 中的 7 更改为 6
  2. s[7] 处添加空终止符]

结果将是:

char s[10] = "qwerty";
s[6] = 'A';
s[7] = 0;

原始不起作用固定工作

至于 strncpymemcpy,区别在于strncpy< /code> 为您添加一个空终止符。但是,仅当源字符串在 n 之前有一个时。所以 strncpy 就是你想要在这里使用的,但要非常小心大的 BUT。

You are getting the output querty because the index 7 is incorrect (arrays are indexed from 0, not 1). There is a null-terminator at index 6 to signal the end of the string, and whatever comes after it will have no effect.

Two things you need to fix:

  1. Change the 7 in s[7] to 6
  2. Add a null-terminator at s[7]

The result will be:

char s[10] = "qwerty";
s[6] = 'A';
s[7] = 0;

Original not working and fixed working.

As for the question of strncpy versus memcpy, the difference is that strncpy adds a null-terminator for you. BUT, only if the source string has one before n. So strncpy is what you want to use here, but be very careful of the big BUT.

浅忆流年 2024-10-17 03:37:29

即使您指定了要复制的字节数,Strncpy 也将复制最多 NULL,但 memcpy 最多会复制指定的字节数。

printf 语句将打印最多 NULL ,因此您将尝试打印单个字符,它将显示 ,

printf("\t%c %c %c\t",s[7],str[7],str1[7]);

OUTPUT

  7              7

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 ,

printf("\t%c %c %c\t",s[7],str[7],str1[7]);

OUTPUT

  7              7
じее 2024-10-17 03:37:29

要制作“qwertyA”,您需要设置 s[6] = 'A's[7]='\0'

字符串从 0 开始索引,因此 s[0] == 'q',并且它们需要以 null 终止。

To make "qwertyA" you need to set s[6] = 'A' and s[7]='\0'

Strings are indexed from 0, so s[0] == 'q', and they need to be null terminated.

别把无礼当个性 2024-10-17 03:37:29

当你有:

char s[10] = "qwerty";

这就是该数组包含的内容:

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

如果你想在字符串末尾添加一个“A”,那就是索引 6,因为数组索引从 0 开始

 s[6] = 'A';

请注意,当你以这种方式初始化数组时,剩余的space 设置为 0(nul 终止符),虽然在这种情况下不需要,但通常要注意您需要使字符串以 nul 终止。
例如,

char s[10];
strcpy(s,"qwerty");
s[6] = 'A';
s[7] = 0;

在上面的示例中,“qwerty”(包括其 nul 终止符)被复制到 s。 s[6] 覆盖 nul 终止符。由于 s 的其余部分尚未初始化,我们需要自己添加一个 nul 终止符 s[7] = 0;

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

 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.

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 s is not initialized we need to add a nul terminator ourselves with s[7] = 0;

半衬遮猫 2024-10-17 03:37:29

正如 Philip Potter 所解释的,主要区别在于 memcpy 将复制您要求的所有 n 个字符,而 strncpy 将复制最多包含第一个空终止符的内容,或 n 个字符,以较少者为准。如果 strncpy 复制的字符少于 N 个字符,它将用空字符填充其余部分。
下面的程序将回答你的问题:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    char s[10] = "qwerty",str1[10];
    int i;
    s[7] = 'A';
    memcpy(str1, s, 8);
    for(i=0;i<8;i++)
    {
        if(str1[i]!='\0')
            printf("%c",str1[i]);
    }
    return 0;
}

o/p:

qwertyA

执行下面的代码并检查差异,你可能会发现它很有用。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char s[10] = "qwer\0ty", 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);
    for(int i = 0; i<8; i++)
    {
        printf("%d=%c,",i,str[i]);
    }
    printf("\n");
    for(int i = 0; i<8; i++)
    {
        printf("%d=%c,",i,str1[i]);
    }
    return 0;
}

输出:

qwer
qwer
qwer
0=q,1=w,2=e,3=r,4=,5=,6=,7=,
0=q,1=w,2=e,3=r,4=,5=t,6=y,7=A,

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:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    char s[10] = "qwerty",str1[10];
    int i;
    s[7] = 'A';
    memcpy(str1, s, 8);
    for(i=0;i<8;i++)
    {
        if(str1[i]!='\0')
            printf("%c",str1[i]);
    }
    return 0;
}

o/p:

qwertyA

Execute the below code and check the difference, you might find it useful.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char s[10] = "qwer\0ty", 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);
    for(int i = 0; i<8; i++)
    {
        printf("%d=%c,",i,str[i]);
    }
    printf("\n");
    for(int i = 0; i<8; i++)
    {
        printf("%d=%c,",i,str1[i]);
    }
    return 0;
}

Output:

qwer
qwer
qwer
0=q,1=w,2=e,3=r,4=,5=,6=,7=,
0=q,1=w,2=e,3=r,4=,5=t,6=y,7=A,
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文