{"id":3853,"date":"2019-11-23T00:01:38","date_gmt":"2019-11-23T08:01:38","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3853"},"modified":"2019-11-16T09:50:02","modified_gmt":"2019-11-16T17:50:02","slug":"functions-as-structure-members","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3853","title":{"rendered":"Functions as Structure Members"},"content":{"rendered":"<p>A programming puzzle kept me awake one night: If a structure allows for any variable type to be a member, and a function is a valid variable type, why not have a structure with a function as one of its members? Am I nuts?<br \/>\n<!--more--><br \/>\nYes, I&#8217;m nuts.<\/p>\n<p>Referring back to a <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1526\">Lesson from 2015<\/a>, functions are variables. All you must do is declare them properly &mdash; <code><em>type<\/em> (*<em>name<\/em>)(<em>arguments<\/em>)<\/code> &mdash; and you can have a function as a member in a structure.<\/p>\n<p>Never mind the why, though I can think of some intriguing reasons, but here is such a structure definition:<\/p>\n<p><code>\tstruct f {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;void (*f1)();<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;void (*f2)();<br \/>\n};<\/code><\/p>\n<p>Structure <em>f<\/em> contains two functions as members, <em>f1<\/em> and <em>f2<\/em>. Like any structure definition, these members are placeholders. The next step is to declare a <em>struct f<\/em> variable and then assign functions to the <em>f1<\/em> and <em>f2<\/em> members. The following code demonstrates:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid left(void)\r\n{\r\n    puts(\"left\");\r\n}\r\n\r\nvoid right(void)\r\n{\r\n    puts(\"right\");\r\n}\r\n\r\nint main()\r\n{\r\n    struct f {\r\n        void (*f1)();\r\n        void (*f2)();\r\n    };\r\n    struct f func;\r\n\r\n    func.f1 = &amp;left;\r\n    func.f2 = &amp;right;\r\n\r\n    func.f1();\r\n    func.f2();\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The silly functions <em>left()<\/em> and <em>right()<\/em> output strings. These functions are assigned to the members <em>f1<\/em> and <em>f2<\/em> of <em>struct f<\/em> variable <code>func<\/code> at Lines 21 and 22. Only the functions&#8217; names are used, prefixed by the address-of (&amp; ampersand) operator. Remember that functions are referenced internally by their addresses.<\/p>\n<p>At Lines 24 and 25, the functions are called &mdash; indirectly via the structure members:<\/p>\n<p><code>func.f1();<br \/>\nfunc.f2();<\/code><\/p>\n<p>And it works! Here&#8217;s the output:<\/p>\n<p><code>left<br \/>\nright<\/code><\/p>\n<p>Keeping in mind that a structure is yet another type of C language variable (I refer to is as a &#8220;multi-variable&#8221; in my books), the potential of mixing data with functions is nifty. In a way, it opens a portal through which you could create almost object-oriented like features in the C language: By attaching functions to a data type, you can access them similar to the way &#8220;methods&#8221; are accessed in object-oriented languages. Of course, a lot of overhead would be involved, but the potential exists.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A structure can hold any valid variable type as a member, so why not have a function as a structure member? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3853\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-3853","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3853","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3853"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3853\/revisions"}],"predecessor-version":[{"id":3865,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3853\/revisions\/3865"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}