{"id":3845,"date":"2024-03-12T01:30:42","date_gmt":"2024-03-11T20:00:42","guid":{"rendered":"https:\/\/cthecosmos.com\/?p=3845"},"modified":"2024-03-12T01:30:42","modified_gmt":"2024-03-11T20:00:42","slug":"common-combinations-and-their-usage-in-c-part-2","status":"publish","type":"post","link":"https:\/\/cthecosmos.com\/?p=3845","title":{"rendered":"Common Combinations and their Usage in C &#8211; Part 2"},"content":{"rendered":"\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-1411517daed9de9778d848b826c52ff8\" style=\"font-size:17px\"><strong>Structure of Unions:<\/strong> Whenever you need different data types to be stored in the same memory. Saves memory space and provides flexibility in handling different data types. Ex: Used for HW configuration where each member in the union corresponds to a specific HW configuration, in protocol parsing, each member in the union can be used to perform specific operations such as parsed, processed etc&#8230; on the data.<br><br><strong>Syntax:<\/strong> <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nUnion uni_on1{ \u2026};\nUnion uni_on2{\u2026};\nstruct struct_of_union{\nunion uni_on1 mem1;\nunion uni_on2 mem2;\n};\nStruct struct_of_union a_struct;\nA_struct.mem1.uni_on1 = some_val;\n<\/pre><\/div>\n\n\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-98e7b62621497637dc89ff3d3d5f04fd\">Ex:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nstruct pmop {\nBASEOP\nOP *\u00a0\u00a0\u00a0op_first;\nOP *\u00a0\u00a0\u00a0op_last;\n\n# ifdef USE_ITHREADS\nPADOFFSET\u00a0\u00a0op_pmoffset;\n#else\nOP *\u00a0\u00a0\u00a0op_pmregexp;\n#endif\n\nU32\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0op_pmflags;\n\nunion {\nOP *\u00a0\u00a0\u00a0op_pmreplroot;\nPADOFFSET op_pmtargetoff;\nGV *\u00a0\u00a0\u00a0op_pmtargetgv;\n}\u00a0\u00a0op_pmreplrootu;\n\nunion {\nOP *\u00a0\u00a0\u00a0op_pmreplstart;\n#ifdef USE_ITHREADS\nPADOFFSET op_pmstashoff;\n#else\nHV *\u00a0\u00a0\u00a0op_pmstash;\n#endif\n}\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0op_pmstashstartu;\n\nOP *\u00a0\u00a0\u00a0op_code_list;\n}; \/\/ Used in Perl regular expression engine to represent a pattern match operation.\n<\/pre><\/div>\n\n\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-3d4e4f64306fb7ec6865cfb394fb3dff\" style=\"font-size:17px\"><br>(\/lib\/x86_64-linux-gnu\/perl\/5.34.0\/CORE\/op.h)<br><br><br><strong>Union of Structures:<\/strong> Used where we need to represent different data types in same memory location. EX: Used in file data strcutures where features such as file size, permission, timestamps are wrapped in a single data structures.<br><br><strong><em>Ex:<\/em><\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nunion fw_cdev_event {\nstruct fw_cdev_event_common\u00a0\u00a0\u00a0\u00a0common;\nstruct fw_cdev_event_bus_reset\u00a0\u00a0\u00a0\u00a0\u00a0bus_reset;\n...\nstruct fw_cdev_event_iso_interrupt_mc\u00a0\u00a0iso_interrupt_mc;\nstruct fw_cdev_event_iso_resource\u00a0\u00a0iso_resource;\nstruct fw_cdev_event_phy_packet\u00a0\u00a0\u00a0\u00a0phy_packet;\n}; \/\/ Used to represent different types of events or messages that can be received or transmitted within the FireWire communication context.\n<\/pre><\/div>\n\n\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-e680e6e435582a8dd9e95dfe5cc826b2\" style=\"font-size:17px\"><br><strong>Structure of union of structure:<\/strong> Used to handle different data formats and interpretations on conditions or modes. Ex: Can be seen its usage in linux kernel sub system network.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nstruct __kernel_sockaddr_storage {\nunion {\nstruct {\n__kernel_sa_family_t\u00a0\u00a0\u00a0ss_family;\nchar __data&#x5B;_K_SS_MAXSIZE - sizeof(unsigned short)];\n};\nvoid *__align; \u00a0\u00a0\u00a0\u00a0\u00a0}; }; (\/\/usr\/include\/linux\/socket.h)\n<\/pre><\/div>\n\n\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-3134d1a71282ff5826c86f177b1bf8e1\" style=\"font-size:17px\"><br><strong>Array of pointers to functions<\/strong>: Used to store and manage collection of different function pointers.<br><strong><em>Syntax:<\/em><\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nchar (*fun_ptr_arr&#x5B;5])(int,char);\nfun_ptr1&#x5B;0] = some_fun1();\nfun_ptr2&#x5B;1] = some_fun2();\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"523\" data-attachment-id=\"3851\" data-permalink=\"https:\/\/cthecosmos.com\/?attachment_id=3851\" data-orig-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/03\/image.png?fit=800%2C523&amp;ssl=1\" data-orig-size=\"800,523\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/03\/image.png?fit=800%2C523&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/03\/image.png?resize=800%2C523&#038;ssl=1\" alt=\"\" class=\"wp-image-3851\" style=\"width:690px;height:auto\" \/><\/figure>\n\n\n\n<p class=\"has-white-color has-text-color has-background has-link-color has-medium-font-size wp-elements-486e7799b35920eece0029316d5e98bd\" style=\"background:linear-gradient(135deg,rgb(35,23,11) 0%,rgb(254,45,45) 50%,rgb(107,0,62) 100%)\">~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br><strong>An Article by: <\/strong>Yashwanth Naidu Tikkisetty<br>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Structure of Unions: Whenever you need different data types to be stored in the same memory. Saves memory space and provides flexibility in handling different data types. Ex: Used for HW configuration where each member in the union corresponds to a specific HW configuration, in protocol parsing, each member in the union can be used [&hellip;]<\/p>\n<a href=\"https:\/\/cthecosmos.com\/?p=3845\" class=\"more-link\">Read More <span class=\"screen-reader-text\">Common Combinations and their Usage in C &#8211; Part 2<\/span><\/a>","protected":false},"author":120055267,"featured_media":0,"comment_status":"open","ping_status":"open","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,"font":"","enabled":false},"version":2},"_wpas_customize_per_network":false},"categories":[769112296,36985],"tags":[769116859,770034012,20863,196,67544,770034011,1332],"class_list":["post-3845","post","type-post","status-publish","format-standard","hentry","category-c-concepts-hub","category-c-programming","tag-cprogram-2","tag-declaration-reading","tag-pointers","tag-programming","tag-structures","tag-syntax-reading","tag-unions","has-post-thumbnail","fallback-thumbnail"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8CiEf-101","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts\/3845","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/users\/120055267"}],"replies":[{"embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3845"}],"version-history":[{"count":7,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts\/3845\/revisions"}],"predecessor-version":[{"id":3853,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts\/3845\/revisions\/3853"}],"wp:attachment":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3845"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3845"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3845"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}