需要帮助来使用 memcpy 处理缓冲区

发布于 2024-11-25 08:03:54 字数 688 浏览 0 评论 0原文

在下面的代码中,当我使用fwrite时,它给出了正确的o/p。虽然 memcpy 不起作用。

typedef struct
{
  char *p1;
  char *p2;
} node;
char s[] = "hello";
char t[] =" there";
node t1, t2;
char str;
FILE op_f;

t1.p1 = malloc(sizeof(strlen(s));
t1.p2 = malloc(sizeof(strlen(s));

t2.p1 = malloc(sizeof(strlen(s));
t2.p2 = malloc(sizeof(strlen(s));

t1.p1 = s;
t1.p2 = t;
copy(&t1,&t2);

str = malloc(sizeof(strlen(s) + strlen(t));
/* gives o/p hello there */
fwrite(t2.p1,1,strlen(s),op_f);
fwrite(t2.p1,2,strlen(s),op_f);

/* gives o/p there */
memcpy(str,t2.p1,strlen(s));
memcpy(str,t2.p2,strlen(s));

有什么方法可以将缓冲区复制到 str 吗?

PS:以上代码仅供参考,并非实际代码

in the following code when i use fwrite its giving correct o/p. While memcpy is not working.

typedef struct
{
  char *p1;
  char *p2;
} node;
char s[] = "hello";
char t[] =" there";
node t1, t2;
char str;
FILE op_f;

t1.p1 = malloc(sizeof(strlen(s));
t1.p2 = malloc(sizeof(strlen(s));

t2.p1 = malloc(sizeof(strlen(s));
t2.p2 = malloc(sizeof(strlen(s));

t1.p1 = s;
t1.p2 = t;
copy(&t1,&t2);

str = malloc(sizeof(strlen(s) + strlen(t));
/* gives o/p hello there */
fwrite(t2.p1,1,strlen(s),op_f);
fwrite(t2.p1,2,strlen(s),op_f);

/* gives o/p there */
memcpy(str,t2.p1,strlen(s));
memcpy(str,t2.p2,strlen(s));

Is there any way to copy buffer to str ??

PS: above code is just for reference not the actual code

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

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

发布评论

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

评论(5

抹茶夏天i‖ 2024-12-02 08:03:54

您应该将 str 声明为 char 数组,而不是单个 char:

char str[500];

当 str 声明为数组时,str 是一个指针(指向数组的第一个元素)。在你的代码中 - str 是一个字符,但不是指针。 Memcpy 需要指针作为第一个和第二个参数

此外,对没有 sizeof 的字符串使用 malloc:

 t1.p2 = malloc(strlen(s)+1);

此外,fwrite 的使用不正确,因为 op_f 没有使用 fopen 初始化,如下所示:

 op_f = fopen("filename.txt", "w");

You should declare str as char array, not a single char:

char str[500];

when str is declared as array, the str is a pointer (to the first element of array). And in your code - str was a char, but not a pointer. Memcpy needs pointers as first and second arguments

Also, use malloc for strings without sizeof:

 t1.p2 = malloc(strlen(s)+1);

Also, fwrite is used incorrectly, because the op_f is not initialized with fopen like:

 op_f = fopen("filename.txt", "w");
一曲爱恨情仇 2024-12-02 08:03:54

存储在 char 数组中的字符串需要一个额外的字节作为终止符。计算大小时,您需要添加 1 个额外位置:

t1.p1 = malloc(strlen(s) + 1); 

此代码

t1.p1 = s; 

不会将文本从 s 复制到 p1,而是将 p1 设置为指向与s相同的字符串。

这部分

str = malloc(strlen(s) + strlen(t));  

不起作用,因为 str 是单个 char 而不是 char*。您可以尝试

char* str = malloc(strlen(s) + strlen(t) + 1);  

如果您确实希望将其标记为 C++,则应该考虑使用 std::string 代替。它处理所有这些细节。

Strings stored in char arrays need one extra byte for a terminator. When computing the size you need to add 1 extra position:

t1.p1 = malloc(strlen(s) + 1); 

This code

t1.p1 = s; 

doesn't copy the text from s to p1, but sets p1 to point to the same string as s.

This part

str = malloc(strlen(s) + strlen(t));  

doesn't work because str is a single char and not a char*. You could try

char* str = malloc(strlen(s) + strlen(t) + 1);  

If you really intend this to be tagged C++, you should consider using std::string instead. It handles all of these details.

清醇 2024-12-02 08:03:54

我刚刚注意到您使用“char str”而不是“char *str”,因此 malloc(指向字符串的指针)的结果没有正确存储在变量中。

另外,当您进行 malloc 时,您需要按以下方式计算大小:strlen(s) + strlen(t) + 1。额外的字节用于终止 NULL 字符。而且您不需要在那里使用 sizeof 。最后的语句将如下所示:

str = malloc(strlen(s) + strlen(t) + 1);

I just noticed you use 'char str' instead of 'char *str', so the result of malloc (which is a pointer to string) it not correctly stored in the variable.

Also, when you malloc, you need to calculate the size the following way: strlen(s) + strlen(t) + 1. The extra byte is for the terminating NULL character. And you don't need to use sizeof there. The finally statement will look like:

str = malloc(strlen(s) + strlen(t) + 1);
甜中书 2024-12-02 08:03:54
/* These are static.  You SHOULD NOT write to either s or t! */
char s[] = "hello";
char t[] =" there";

typedef struct
{
  char *p1;
  char *p2;
} node;
node t1, t2;

/* I think you wanted "*str" here... */
char *str;

/* You definitely want "length of string + 1" here */
t1.p1 = malloc(strlen(s) + 1);
strcpy (t1.p1, s);
...
/* strcpy() and strcat() might be applicable here */
/* strncpy() and strncpy() might be even better - it depends... */
str = malloc(strlen(s)+1 + strlen(t)+1);
strcpy (str, s);
strcat (str, t);
/* These are static.  You SHOULD NOT write to either s or t! */
char s[] = "hello";
char t[] =" there";

typedef struct
{
  char *p1;
  char *p2;
} node;
node t1, t2;

/* I think you wanted "*str" here... */
char *str;

/* You definitely want "length of string + 1" here */
t1.p1 = malloc(strlen(s) + 1);
strcpy (t1.p1, s);
...
/* strcpy() and strcat() might be applicable here */
/* strncpy() and strncpy() might be even better - it depends... */
str = malloc(strlen(s)+1 + strlen(t)+1);
strcpy (str, s);
strcat (str, t);
总以为 2024-12-02 08:03:54

首先,在处理字符串时(如果它们以 null 结尾),您应该使用 strcpy ,因为您的代码当前存在一个错误,该错误被“hello”和“there”长度相同的事实所隐藏,要修复它,您应该这样做(同样适用于 malloc 调用):

fwrite(t2.p1,sizeof(char),strlen(t2.p1),op_f);
fwrite(t2.p2,sizeof(char),strlen(t2.p2),op_f); //was also a bug here, you used p1 instead of p2 and the size of each element should have been 1

/* gives o/p there */
memcpy(str,t2.p1,strlen(t2.p1));
memcpy(str,t2.p2,strlen(t2.p2));

您真正的问题源于 memcpy 不会增加指针,因此您应该这样做:

strcpy(str,t2.p1);
strcat(str,t2.p2);

或如果你真的想用memcpy:

memcpy(str,t2.p1,strlen(t2.p1));
memcpy(str + strlen(t2.p1) - 1,t2.p2,strlen(t2.p2));

最后,您的 malloc 的返回指针,而不是 char,因此 str 应该是 char*

firstly, you should be using strcpy when working with strings (if they are null terminated), as your code currently has a bug which is hidden by the fact 'hello' and 'there' are the same length, to fix it you should be doing (same applies to the malloc calls):

fwrite(t2.p1,sizeof(char),strlen(t2.p1),op_f);
fwrite(t2.p2,sizeof(char),strlen(t2.p2),op_f); //was also a bug here, you used p1 instead of p2 and the size of each element should have been 1

/* gives o/p there */
memcpy(str,t2.p1,strlen(t2.p1));
memcpy(str,t2.p2,strlen(t2.p2));

Your real problem originates because memcpy doesn't increment pointers, hence you should be doing:

strcpy(str,t2.p1);
strcat(str,t2.p2);

or if you really want to use memcpy:

memcpy(str,t2.p1,strlen(t2.p1));
memcpy(str + strlen(t2.p1) - 1,t2.p2,strlen(t2.p2));

finally, your malloc's return pointers, not a char, so str should be a char*.

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