Struct assignment (Just a tip)

    Both standard C and Standard C++ support struct assignment using = . In fact, C++ programmers are often surprised to discover that assignment operator applies also to plain structs, and not just to objects, and C programmers are even more puzzled to discover they don’t have to assign structs tediously, field-by-field when they have a much better alternative:

        9.2: I heard that structures could be assigned to variables and passed to and from functions, but K&R I says not.

What K&R I said was that the restrictions on struct operations would be lifted in a forthcoming version of the compiler, and in fact struct assignment and passing were fully functional in Ritchie's compiler even as K&R I was being published.  Although a few early C compilers lacked struct assignment, all modern compilers support it, and it is part of the ANSI C standard, so there should be no reluctance to use it.

 

Yes, structure assignment is fine. But if you have pointers in the struct,
you might need to make copies of the data pointed to (a so-called "deep
copy"). (An alternative is reference counting, but that's a bit more
complicated.)

 

so, feel free to use "=" when assigning structs.

原文链接: https://www.cnblogs.com/yangyingchao/archive/2011/12/09/2281763.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    Struct assignment (Just a tip)

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/38178

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 下午2:49
下一篇 2023年2月8日 下午2:50

相关推荐