内存设置+空白 +内存复制

发布于 2024-12-26 23:07:47 字数 305 浏览 1 评论 0原文

我如何将一个大小为 100 的字符数组设置为空白,然后将 10 个字符从其他字符复制到同一字符串。 例如:

有一个char数组a[100] 要做的事:将其全部设置为空白

现在有另一个数组:b[10](假设它填充了一些长度为 9 的字符串) 要做的事:将此数组复制到前一个数组

What iam doing is : memset(a, ' ', sizeof(a));
350         memcpy(a,b, strlen(b))

,但是我在复制 10 个字符后没有获得我设置的空间。

How can i set a character array say of size 100 to whitespace and then copy 10 charters to that same string from other.
For example:

there is one char array a[100]
To do : set all of it to whitespace

Now there is another array : b[10] (suppose this is filled with some string of length 9)
To do : copy this array to the previous one

What iam doing is : memset(a, ' ', sizeof(a));
350         memcpy(a,b, strlen(b))

But iam not getting space that i had set after 10 chars has been copied .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

掩饰不了的爱 2025-01-02 23:07:47

尝试以下操作:

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

#define LENGTH 100

int main(int argc, char *argv[])
{
    char *a = NULL;
    char b[10] = "abcdefghi"; /* note that this has a trailing null character */

    a = malloc(LENGTH + 1);
    if (a) {
        *(a + LENGTH) = '\0';
        memset(a, ' ', LENGTH);
        fprintf(stdout, "a (before):\t[%s]\n", a);
        memcpy(a, b, sizeof(b) - 1); /* strip trailing null */
        fprintf(stdout, "a (after):\t[%s]\n", a);
        free(a);
    }

    return EXIT_SUCCESS;
}

运行此命令:

$ gcc -Wall test.c
$ ./a.out
a (before):     [...100 spaces...........]
a (after):      [abcdefghi...91 spaces...]                                                                                           ]

Try the following:

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

#define LENGTH 100

int main(int argc, char *argv[])
{
    char *a = NULL;
    char b[10] = "abcdefghi"; /* note that this has a trailing null character */

    a = malloc(LENGTH + 1);
    if (a) {
        *(a + LENGTH) = '\0';
        memset(a, ' ', LENGTH);
        fprintf(stdout, "a (before):\t[%s]\n", a);
        memcpy(a, b, sizeof(b) - 1); /* strip trailing null */
        fprintf(stdout, "a (after):\t[%s]\n", a);
        free(a);
    }

    return EXIT_SUCCESS;
}

Running this:

$ gcc -Wall test.c
$ ./a.out
a (before):     [...100 spaces...........]
a (after):      [abcdefghi...91 spaces...]                                                                                           ]
避讳 2025-01-02 23:07:47

您在数组的最后一个位置缺少空 \0 字符。

You are missing the null \0 character at the last position of the array.

天邊彩虹 2025-01-02 23:07:47

你会看到的。尝试以下代码:

for (int i = 0; i <= 99; i++)
    printf("%c", a[i]);
printf("\n");

原因是当您memcpy字符串到a时,一个NULL字符被放置在a[10]中。如果只输出a,输出将在NULL之前停止。

You'll see. Try the following code:

for (int i = 0; i <= 99; i++)
    printf("%c", a[i]);
printf("\n");

The reason is when you memcpy the string to a, a NULL character is placed in a[10]. If you only output a, the output will stop just before the NULL.

梦一生花开无言 2025-01-02 23:07:47

您不认为您需要将类型转换为 (char *)

a = (char *) malloc(LENGTH + 1);

don't you think that you need to type cast to (char *)

a = (char *) malloc(LENGTH + 1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文