使用memcpy()将一个结构复制到另一个结构 [英] use memcpy() to copy one structure to another
问题描述
大家好。
我可以这样做来复制struct b中struct a的内容吗
struct myStruct a,b, * aptr,* bptr;
aptr = a;
bptr = b;
做一些事情来初始化结构的内容a
memcpy(bptr,aptr,sizeof(myStruct));
或者假设使用内存并不是一个好主意
by my结构是连续的吗?
我应该逐场进行。
Kev。
Hi all.
Can I do this to duplicate the contents of struct a in struct b
struct myStruct a, b, *aptr, *bptr;
aptr = a;
bptr = b;
Do something to initialize contents of structure a
memcpy(bptr, aptr, sizeof(myStruct));
Or is not not a good idea to assume the memory used
by my structure is contiguous ?
should I instead do it field by field.
Kev.
推荐答案
Sourcerer写道:
Sourcerer wrote:
大家好。
我可以这样做来复制struct b中struct a的内容
struct myStruct a,b,* aptr,* bptr;
aptr = a;
非法。你的意思是& a?
bptr = b;
Ditto& b。
做一些事情来初始化结构的内容a
memcpy(bptr,aptr,sizeof(myStruct));
或者假设我的结构使用的内存是连续的并不是一个好主意?
它可能有差距,但sizeof将涵盖所有。但是为什么不用
memcpy(bptr,aptr,sizeof(* bptr));
如果你使用memcpy设置了
?
我应该逐字逐句。
Hi all.
Can I do this to duplicate the contents of struct a in struct b
struct myStruct a, b, *aptr, *bptr;
aptr = a;
Illegal. Did you mean &a?
bptr = b;
Ditto &b.
Do something to initialize contents of structure a
memcpy(bptr, aptr, sizeof(myStruct));
Or is not not a good idea to assume the memory used
by my structure is contiguous ?
It may have gaps, but sizeof will cover them all. But why not
memcpy( bptr, aptr, sizeof (*bptr) );
if you''re set on using memcpy?
should I instead do it field by field.
$ b $有什么问题ba = b;
?
-
Chris" electric hedgehog" Dollin
C常见问题解答: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C欢迎: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Sourcerer< no **** @ donteventhinkaboutspammingme.xyz>写道:
Sourcerer <no****@donteventhinkaboutspammingme.xyz> writes:
大家好。
我可以这样做来复制struct b中struct a的内容吗
struct myStruct a ,b,* aptr,* bptr;
aptr = a;
bptr = b;
做一些事情来初始化结构的内容a
memcpy(bptr,aptr,sizeof(myStruct) ));
Hi all.
Can I do this to duplicate the contents of struct a in struct b
struct myStruct a, b, *aptr, *bptr;
aptr = a;
bptr = b;
Do something to initialize contents of structure a
memcpy(bptr, aptr, sizeof(myStruct));
memcpy(& b,& a,sizeof b);或简单地b = a;
memcpy (&b, &a, sizeof b); or simply b = a;
2004年6月15日星期二14:13:22 +0100,Chris Dollin< ke ** @ hpl.hp.com> < br $>
写道:
On Tue, 15 Jun 2004 14:13:22 +0100, Chris Dollin <ke**@hpl.hp.com>
wrote:
Sourcerer写道:
Sourcerer wrote:
大家好。
我可以做吗?这是复制struct b中struct a的内容
struct myStruct a,b,* aptr,* bptr;
aptr = a;
Hi all.
Can I do this to duplicate the contents of struct a in struct b
struct myStruct a, b, *aptr, *bptr;
aptr = a;
>非法。你的意思是& a?
Illegal. Did you mean &a?
bptr = b;
bptr = b;
同上& b。
Ditto &b.
做一些事情来初始化结构的内容a
memcpy(bptr,aptr,sizeof(myStruct));
或者假设使用的内存不是一个好主意
我的结构是连续的吗?
Do something to initialize contents of structure a
memcpy(bptr, aptr, sizeof(myStruct));
Or is not not a good idea to assume the memory used
by my structure is contiguous ?
它可能有差距,但sizeof将覆盖它们。但是为什么不用memcpy(bptr,aptr,sizeof(* bptr));
如果你开始使用memcpy?
It may have gaps, but sizeof will cover them all. But why not
memcpy( bptr, aptr, sizeof (*bptr) );
if you''re set on using memcpy?
我应该逐个字段地进行。
should I instead do it field by field.
a = b;
>?
哇!那很快。
我没有意识到a = b会这样做。
所以,我可以有
struct mystruct
{
char str [50];
}
struct mystruct a,b;
sprintf(a.str," Testing 123");
b = a;
然后b.str还包含Testing 123。 ?
Wow ! that was fast.
I hadn''t realized a = b would do it.
so, I could have
struct mystruct
{
char str[50];
}
struct mystruct a, b;
sprintf(a.str, "Testing 123");
b = a;
and then b.str would also contain "Testing 123" ?
这篇关于使用memcpy()将一个结构复制到另一个结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!