{"id":3854,"date":"2024-03-12T01:44:47","date_gmt":"2024-03-11T20:14:47","guid":{"rendered":"https:\/\/cthecosmos.com\/?p=3854"},"modified":"2024-03-12T01:44:47","modified_gmt":"2024-03-11T20:14:47","slug":"sample-program-for-declaration-reading","status":"publish","type":"post","link":"https:\/\/cthecosmos.com\/?p=3854","title":{"rendered":"Sample Program for Declaration Reading"},"content":{"rendered":"\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-e29226e4a6a6069eda886d3a346f26b3\" style=\"font-size:17px\">This a sample program to demonstrate the use of various declarations that are used. There are indeed many more that are used based on given situation, but these are references on how few of them are used.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\n#include &amp;lt;stdio.h&amp;gt;\n#include &amp;lt;stdlib.h&amp;gt;\n\nint my_add(int a, int b) { return a + b; }\nint my_sub(int a, int b) { return a - b; }\nint my_mul(int a, int b) { return a * b; }\nint my_div(int a, int b) { return a \/ b; } \n\ntypedef struct {\n int x;\n int y;\n} Point;\n\nPoint* createPoint(int x, int y) {\n Point* p = (Point*)malloc(sizeof(Point));\n p-&amp;gt;x = x;\n p-&amp;gt;y = y;\n return p;\n}\n\ntypedef int (*Operation_Func_ptr)(int, int); \/\/ Function pointer typedef\n\nint perform_operation(int a, int b, Operation_Func_ptr operation) {\n return operation(a, b);\n}\n\nint getXCoordinate(Point* p) {\n return p-&amp;gt;x;\n}\n\nint main() {\n int a = 200, b = 25;\n int result;\n\n int (*operations_array_of_ptr_to_fun&#x5B;4])(int, int) = { my_add, my_sub, my_mul, my_div }; \/\/ Array of pointers to functions\n\n Point points&#x5B;] = { {1, 2}, {3, 4}, {5, 6} };\n Point* p = points; \/\/ Pointer to array of structures\n\n Point* p1 = createPoint(10, 20); \/\/ Pointer to function returning a structure\n\n typedef union {\n int intValue;\n float floatValue;\n } Data;\n\n Data* data = (Data*)p1; \/\/ Pointer to union\n\n Point* (*createPointPtr)(int, int) = createPoint;\n Point* p2 = createPointPtr(50, 90); \/\/ Pointer to function returning a pointer\n\n Point** pp = &amp;amp;p2; \/\/ Pointer to pointer to structure\n\n Point* pointArray&#x5B;] = { p1, p2 }; \/\/ Array of pointers to structures\n\n Point** ppArray = pointArray; \/\/ Pointer to array of pointers\n\n Point* (*createPointPtr2)(int, int) = createPoint; \/\/ Pointer to function returning a pointer to a structure\n Point* p3 = createPointPtr2(90, 40);\n\n int (*getX)(Point*) = getXCoordinate;\n int x = getX(p3); \/\/ Pointer to function taking a pointer to a structure.\n\n typedef struct {\n union {\n int intValue;\n float floatValue;\n } data;\n Point point;\n } UnionStruct; \/\/ Structure of union of structures\n\n UnionStruct unionStruct;\n\n for (int i = 0; i &amp;lt; 4; i++) {\n char* operationNames&#x5B;] = { &quot;my_add&quot;, &quot;my_sub&quot;, &quot;my_mul&quot;, &quot;my_div&quot; };\n printf(&quot;%s = %d\\n&quot;, operationNames&#x5B;i], perform_operation(a, b, operations_array_of_ptr_to_fun&#x5B;i]));\n }\n\n printf(&quot;p1: (%d, %d)\\n&quot;, p1-&amp;gt;x, p1-&amp;gt;y);\n printf(&quot;p2: (%d, %d)\\n&quot;, p2-&amp;gt;x, p2-&amp;gt;y);\n printf(&quot;p3.x = %d\\n&quot;, x);\n printf(&quot;Union Data: %d\\n&quot;, unionStruct.data.intValue);\n printf(&quot;Union Point: (%d, %d)\\n&quot;, unionStruct.point.x, unionStruct.point.y);\n\n return 0;\n}\n<\/pre><\/div>\n\n\n<p><\/p>\n\n\n\n<p><\/p>\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>This a sample program to demonstrate the use of various declarations that are used. There are indeed many more that are used based on given situation, but these are references on how few of them are used. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~An Article by: Yashwanth Naidu Tikkisetty~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<\/p>\n<a href=\"https:\/\/cthecosmos.com\/?p=3854\" class=\"more-link\">Read More <span class=\"screen-reader-text\">Sample Program for Declaration Reading<\/span><\/a>","protected":false},"author":120055267,"featured_media":3857,"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":[140212547,772321195,21201225,7481078,20863,67544,93280],"class_list":{"0":"post-3854","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","6":"hentry","7":"category-c-concepts-hub","8":"category-c-programming","9":"tag-c-progra","10":"tag-c-programming","11":"tag-code-reading","12":"tag-coding-practice","13":"tag-pointers","14":"tag-structures","15":"tag-syntax","17":"fallback-thumbnail"},"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/03\/sample-code.webp?fit=1024%2C1024&ssl=1","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8CiEf-10a","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts\/3854","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=3854"}],"version-history":[{"count":3,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts\/3854\/revisions"}],"predecessor-version":[{"id":3858,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts\/3854\/revisions\/3858"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/media\/3857"}],"wp:attachment":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}