{"id":1526,"date":"2015-08-29T00:01:31","date_gmt":"2015-08-29T07:01:31","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1526"},"modified":"2015-08-22T09:32:38","modified_gmt":"2015-08-22T16:32:38","slug":"c-language-neat-tricks-2-part-ii","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1526","title":{"rendered":"C Language Neat Tricks #2 (Part II)"},"content":{"rendered":"<p>You could call it a neat trick or just something so oddball insane that few programmers would dare breech the territory: A variable that holds the address of a function; a function pointer.<br \/>\n<!--more--><br \/>\nPointers are scary enough in C, but a function pointer variable?<\/p>\n<p>In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1517\">last week&#8217;s Lesson<\/a>, I showed how one function works as an argument to some other function. That operation takes place because a function has an address, a location in memory. That&#8217;s a pointer!<\/p>\n<p>It&#8217;s possible in C to create a function pointer &mdash; a variable that holds the address of a function. This possibility begs the question: <em>Why<\/em>?<\/p>\n<p>Because this post, like the last two, is about oddball things in C that most programmers don&#8217;t think of. This is definitely an item some interloping Python programmer would consider as insane. And that&#8217;s fine by me.<\/p>\n<p>The format to declare a function variable looks like this:<\/p>\n<p><code><em>ret_type<\/em> (*<em>name<\/em>)(<em>arg_type<\/em>);<\/code><\/p>\n<p><em>ret_type<\/em> is the function&#8217;s return value, such as <em>int<\/em>, <em>void<\/em>, or whatever.<\/p>\n<p><em>name<\/em> is the function&#8217;s name as a variable. It&#8217;s a pointer, so the asterisk is prefixed to the name. Because of the order of precedence, the name and asterisk must be enclosed in parentheses.<\/p>\n<p><em>arg_type<\/em> is the list of function arguments, just as you&#8217;d find in the function&#8217;s prototype. I recommend specifying only the types (<em>int<\/em>, <em>void<\/em>, and so on), not specific variable names.<\/p>\n<p>As with all pointer variables, you must initialize the function pointer to a function&#8217;s address to use it. After that, you can use the function pointer&#8217;s name to reference the function.<\/p>\n<p>Here&#8217;s your sample code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint funct1(void)\r\n{\r\n    return(42);\r\n}\r\n\r\nint funct2(void)\r\n{\r\n    return(24);\r\n}\r\n\r\nint main()\r\n{\r\n    int (*fp)(void);        <span class=\"comments\">\/* variable fp is a function pointer *\/<\/span>\r\n\r\n    fp = funct1;\r\n    printf(\"The solution is %d\\n\",fp());\r\n    fp = funct2;\r\n    printf(\"The solution is %d\\n\",fp());\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The output:<\/p>\n<pre><code>The solution is 42\r\nThe solution is 24<\/code><\/pre>\n<p>The function pointer <code>fp<\/code> is declared in Line 15. It&#8217;s assigned the address of <em>funct1()<\/em> at Line 17, then called in Line 18. It&#8217;s assigned to <em>funct2()<\/em> at Line 19, and used again at Line 20.<\/p>\n<p>Now to seriously address the interloping Python programmer&#8217;s inquiry: <em>Why bother<\/em>?<\/p>\n<p>Honestly, there is absolutely no reason to use a function pointer as shown in the sample code.<\/p>\n<p>The only time you need to use a function pointer construction is when your code contains a function that requires another function as an argument &mdash; as is done in the <em>qsort()<\/em> function. In that case, the following format is implemented in the function&#8217;s declaration as well as inside the function. For example:<\/p>\n<p><code>void qsort(void *base, size_t nel, size_t width, int (*compar)(const void *a, const void *b));<\/code><\/p>\n<p>Inside the function, the variable <code>compar(a,b)<\/code> is used to call the user-define function that compares the two values for the sort, which is just how the variable <code>fp()<\/code> is used in this Lesson&#8217;s sample code.<\/p>\n<p>That&#8217;s it!<\/p>\n<p>Any other insanity you want to try using the function pointer technique is purely up to you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating a variable that holds a function. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1526\">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-1526","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\/1526","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=1526"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1526\/revisions"}],"predecessor-version":[{"id":1542,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1526\/revisions\/1542"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}