{"id":769,"date":"2009-03-10T13:16:48","date_gmt":"2009-03-10T18:16:48","guid":{"rendered":"http:\/\/bitpost.com\/news\/?p=769"},"modified":"2011-04-06T18:21:06","modified_gmt":"2011-04-06T23:21:06","slug":"my-first-c-vs-c99-collision","status":"publish","type":"post","link":"https:\/\/bitpost.com\/news\/2009\/my-first-c-vs-c99-collision\/","title":{"rendered":"My first C++ vs. C99 collision"},"content":{"rendered":"<p>Recently, I found myself kind of annoyed by the fact that I couldn&#8217;t use the same &#8220;initialization&#8221; syntax to directly set the values of an existing structure.  Extremely trivial, but it bugs me.  Is there really a good reason for this limitation?<\/p>\n<pre><code>\ttypedef struct\r\n\t{\r\n\t    int x;\r\n\t    int y;\r\n\r\n\t} Doh;\r\n\r\n    Doh doh = { 1, 2 };\r\n\r\n    \/\/ You can't directly re-assign in one step, bummer.\r\n    \/\/ doh = { 3, 4 };\r\n\r\n    \/\/ You need a second struct to use the same syntax.  Yuck.\r\n    Doh d2 = { 3, 4 };   \r\n    doh = d2;\r\n\r\n    \/\/ Or just do it longhand.  Also yuck.\r\n    doh.x = 5;\r\n    doh.y = 6;\r\n<\/code><\/pre>\n<p>So I dug around to see if there was anything I was missing, and I found <strong>designated initializers<\/strong>, the new initialization method available in C99.  It doesn&#8217;t allow me to directly assign values to an existing structure, but it is interesting:<\/p>\n<pre><code>\tDoh doh_set = { \r\n\t    .x = 4,\r\n\t    .y = 3\r\n\t};\r\n\tDoh doh_set2 = { \r\n\t    .y = 3, \r\n\t    .x = 4 \r\n\t};\r\n\tDoh doh_set3 = { \r\n\t    .y = 3\r\n\t};\r\n    Doh set4[]=\r\n    {\r\n        {\r\n            .y  = 1039      \r\n        },\r\n        {\r\n            .y  = 1040,\r\n            .x  = 23\r\n        }\r\n    };<\/code><\/pre>\n<p>Still not rocket science, but the truly interesting part is that designated initializers are <strong>not<\/strong> yet available in C++.  At least not today.  I tested it with gcc 4.1.2 and Visual Studio 2008 C++ compilers and they do not support it.  It may appear in <a href=\"http:\/\/en.wikipedia.org\/wiki\/C%2B%2B0x\">C++0x<\/a>, but for now, C is definitely no longer a pure subset of C++.  For more possible gotchas (and some C99 features that make it <strong>more<\/strong> compatible with C++), here&#8217;s a <a href=\"http:\/\/www.informit.com\/guides\/content.aspx?g=cplusplus&#038;seqNum=215\">quick C99 rundown<\/a>.<\/p>\n<p>Considering designated initializers are being used in places like the linux kernel, this issue no longer seems trivial.  Oh well, code and learn.  Hopefully I can go another 10 years(!) before my next snag.  For now, back to <strong>classes<\/strong>&#8230;  :><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently, I found myself kind of annoyed by the fact that I couldn&#8217;t use the same &#8220;initialization&#8221; syntax to directly set the values of an existing structure. Extremely trivial, but it bugs me. Is there really a good reason for this limitation? typedef struct { int x; int y; } Doh; Doh doh = { [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[22],"tags":[37],"class_list":["post-769","post","type-post","status-publish","format-standard","hentry","category-cpp","tag-c0x"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9M11L-cp","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts\/769","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/comments?post=769"}],"version-history":[{"count":12,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts\/769\/revisions"}],"predecessor-version":[{"id":1126,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts\/769\/revisions\/1126"}],"wp:attachment":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/media?parent=769"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/categories?post=769"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/tags?post=769"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}