通过单独复制每个成员 ,这是最可靠的方法。示例: struct Object object = allocate_object(); struct Widget小部件; widget.wmember_one = object.omember_one; widget.wmember_two = object.omember_two; / *以这种方式继续,直到你完成* / 更好的方法,恕我直言,将两个结构放入一个 联合,分配第一个,并从第二个读取;虽然 这可能是不合格的(有人会告诉我们 如果是:-) union Both { struct Object object; struct Widget小部件; }; struct Widget小部件; Both.object = allocate_object(); widget = Both.widget; 为什么你会期望这样做做任何明智的事情? 如果`struct Widget''和`struct Object''不相关, struct Object { int this this,那个,the_other; const char * canonical_name; }; struct Widget { struct Widget * link; double centroid [3]; double coeff_matrix [4] [4]; ColorCode hue; }; ....作业有什么意义? 如果*是*某种 `struct Object''和`struct Widget'之间的关系,描述它是什么样的b $ b和某人可能有所帮助 - 但是对于任意 不相关类型的结构,任务很愚蠢。 - Er ********* @ sun.com 2005年6月6日星期一14:40:57 -0400,在comp.lang.c,Eric Sosman < er ********* @ sun.com>写道: 为什么你会期望这样做有意义呢? 我想这可能来自某种形式伪OO环境,你创建一个通用对象句柄,然后将它强制转换为某种类型。你在$ 32 $ b中一直在Win32 API中看到这个。 对于OP:这个分配是没有意义的。我怀疑你需要 分配指针,而不是对象? struct Widget * p_widget =(struct Widget *)& object; 并记住这可能不合法C,因为 对象的成员与* p_widget的成员不一样。 - Mark McIntyre CLC FAQ< http://www.eskimo.com/~scs/C-faq/top.html> CLC自述文件:< http://www.ungerhu.com/jxh/clc.welcome.txt> ---- ==通过新闻源发布。 Com - Unlimited-Uncensored-Secure Usenet News == ---- http:// www .newsfeeds.com 世界排名第一的新闻组服务! 120,000多个新闻组 ---- =东海岸和西海岸服务器农场 - 通过加密实现全隐私= ---- Hi,How do i easily assign one struct to another of a different type? struct Object object = allocate_object(); struct Widget widget = (struct Widget)object; /* doesn''t work */ struct Widget is bigger than struct Object. 解决方案 Short answer, possibly not what you are looking for: memcpy (&widget, &object, sizeof widget); Long answer: The surest way of doing this by copying each memberindividually. Example: struct Object object = allocate_object (); struct Widget widget; widget.wmember_one = object.omember_one;widget.wmember_two = object.omember_two;/* go on in this fashion until you are done */ A better way, IMHO, is to place the two structs into aunion, assign the first, and read from the second; althoughthis is possibly non-conforming (someone will let us knowhere if it is:-) union Both {struct Object object;struct Widget widget;}; struct Widget widget; Both.object = allocate_object ();widget = Both.widget; Why would you expect this to do anything sensible?If `struct Widget'' and `struct Object'' are unrelated, struct Object {int this, that, the_other;const char *canonical_name;}; struct Widget {struct Widget *link;double centroid[3];double coeff_matrix[4][4];ColorCode hue;}; .... what meaning would the assignment have? If there *is* some kind of relationship between`struct Object'' and `struct Widget'', describe what it''slike and someone may be able to help -- but for arbitrarystructs of unrelated types, assignment is silly. -- Er*********@sun.com Why would you expect this to do anything sensible? I imagine this arises from some sort of pseudo OO environment, whereyou create a generic object handle, then coerce it to some type. Yousee this all the time in the Win32 API. To the OP: the assigment makes no sense. I suspect you need to beassigning pointers, not objects? struct Widget* p_widget = (struct Widget*) &object; and bear in mind that this may not be legal C, since the members ofobject will not be the same as those of *p_widget.--Mark McIntyreCLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt> ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups----= East and West-Coast Server Farms - Total Privacy via Encryption =---- 这篇关于按值语义复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-20 10:53