需要帮助来使用 memcpy 处理缓冲区
在下面的代码中,当我使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您应该将 str 声明为 char 数组,而不是单个 char:
当 str 声明为数组时,str 是一个指针(指向数组的第一个元素)。在你的代码中 - str 是一个字符,但不是指针。 Memcpy 需要指针作为第一个和第二个参数
此外,对没有 sizeof 的字符串使用 malloc:
此外,fwrite 的使用不正确,因为
op_f
没有使用fopen
初始化,如下所示:You should declare str as char array, not a single char:
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:
Also, fwrite is used incorrectly, because the
op_f
is not initialized withfopen
like:存储在 char 数组中的字符串需要一个额外的字节作为终止符。计算大小时,您需要添加 1 个额外位置:
此代码
不会将文本从
s
复制到p1
,而是将p1
设置为指向与s
相同的字符串。这部分
不起作用,因为 str 是单个
char
而不是char*
。您可以尝试如果您确实希望将其标记为 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:
This code
doesn't copy the text from
s
top1
, but setsp1
to point to the same string ass
.This part
doesn't work because str is a single
char
and not achar*
. You could tryIf you really intend this to be tagged C++, you should consider using
std::string
instead. It handles all of these details.我刚刚注意到您使用“char str”而不是“char *str”,因此 malloc(指向字符串的指针)的结果没有正确存储在变量中。
另外,当您进行 malloc 时,您需要按以下方式计算大小:strlen(s) + strlen(t) + 1。额外的字节用于终止 NULL 字符。而且您不需要在那里使用 sizeof 。最后的语句将如下所示:
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:
首先,在处理字符串时(如果它们以 null 结尾),您应该使用
strcpy
,因为您的代码当前存在一个错误,该错误被“hello”和“there”长度相同的事实所隐藏,要修复它,您应该这样做(同样适用于malloc
调用):您真正的问题源于
memcpy
不会增加指针,因此您应该这样做:或如果你真的想用memcpy:
最后,您的
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 themalloc
calls):Your real problem originates because
memcpy
doesn't increment pointers, hence you should be doing:or if you really want to use memcpy:
finally, your
malloc
's return pointers, not achar
, sostr
should be achar*
.