{"id":1517,"date":"2015-08-22T00:01:06","date_gmt":"2015-08-22T07:01:06","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1517"},"modified":"2015-08-29T20:35:44","modified_gmt":"2015-08-30T03:35:44","slug":"c-language-neat-tricks-2-part-i","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1517","title":{"rendered":"C Language Neat Tricks #2 (Part I)"},"content":{"rendered":"<p>Like a variable in the C language, a function has an address &mdash; a location in memory. This fact shouldn&#8217;t be surprising to you. After all, the function has to sit somewhere. And my guess is that location doesn&#8217;t change as the program runs.<br \/>\n<!--more--><br \/>\nA function&#8217;s address is an attribute used as an argument in several C language functions. You may have seen this requirement in some C Library functions and perhaps even used a function address in your own code.<\/p>\n<p>As an example, the <em>qsort()<\/em> function&#8217;s fourth argument is the name of the function that compares the values:<\/p>\n<pre><code>qsort(qarray,SIZE,sizeof(int),compare);<\/code><\/pre>\n<p>Above, <code>compare<\/code> is the name of the <em>compare()<\/em> function, which compares two values. (See my <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1258\">Lesson<\/a> on the Quicksort.)<\/p>\n<p>So how does that work?<\/p>\n<p>Look at the <em>qsort()<\/em> statement above. When a function is used as an argument, the parentheses are absent. With the parentheses, the function is called and it does whatever. Minus the parentheses, the function&#8217;s address is returned.<\/p>\n<p>Here&#8217;s sample code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint function(void)\r\n{\r\n    return(42);\r\n}\r\n\r\nint main()\r\n{\r\n    printf(\"The value of function() is %d\\n\",\r\n            function());\r\n    printf(\"The address of function() is %p\\n\",\r\n            function);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>In the first <em>printf()<\/em> statement, the value generated by <em>function()<\/em> is returned.<\/p>\n<p>In the second <em>printf()<\/em> statement, the address of <em>function()<\/em> is returned.<\/p>\n<p>Here&#8217;s how the output looks on my computer:<\/p>\n<pre><code>The value of function() is 42\r\nThe address of function() is 0x10e95ce90<\/code><\/pre>\n<p>Obviously, the address shown on your screen will be different and output in a different format as well. The point is that without the parentheses, the compiler reads function as a memory location, a pointer.<\/p>\n<p>As a pointer, a function also sports a variable type, which makes sense. If you want to get really weird, you can save a function&#8217;s address in a pointer variable and use that variable to call the function. I&#8217;ll show you that truly oddball construction in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1526\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Grabbing the address of a function. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1517\">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-1517","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\/1517","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=1517"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1517\/revisions"}],"predecessor-version":[{"id":1544,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1517\/revisions\/1544"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}