{"id":2029,"date":"2010-07-18T13:42:18","date_gmt":"2010-07-18T17:42:18","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/?p=2029"},"modified":"2010-07-18T13:42:18","modified_gmt":"2010-07-18T17:42:18","slug":"reference-passing-functions-as-parameters-in-c","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/reference-passing-functions-as-parameters-in-c\/","title":{"rendered":"Reference: Passing functions as parameters in C"},"content":{"rendered":"<p>[c]<br \/>\n#include &lt;stdio.h&gt;<br \/>\n\/\/Example to show how to pass<br \/>\n\/\/functions as parameters in C.                                               <\/p>\n<p>\/\/simple function that returns the sum of two ints<br \/>\nint sum(int i, int j) {<br \/>\n  return i+j;<br \/>\n}<\/p>\n<p>\/\/simple function that prints a char*<br \/>\nvoid printSomething(char* something) {<br \/>\n  printf(&quot;%sn&quot;,something);<br \/>\n}<\/p>\n<p>\/\/function that takes<br \/>\n\/\/ 2 ints<br \/>\n\/\/ 1 char*<br \/>\n\/\/ one function that returns an int and takes two ints<br \/>\n\/\/ one function that takes a char* returns nothing<br \/>\nvoid functionThatTakesOtherFunctions(int a,<br \/>\n                int b,<br \/>\n                char* name,<br \/>\n                int (functionA) (int,int),<br \/>\n                void (functionB) (char*)) {<br \/>\n  printf(&quot;Function A: %dn&quot;,functionA(a,b));<br \/>\n  functionB(name);<br \/>\n}<\/p>\n<p>int main(void) {<br \/>\n  \/\/we pass the first two functions as parameters<br \/>\n  functionThatTakesOtherFunctions(3,4,<br \/>\n                                  &quot;John Doe&quot;,<br \/>\n                                  sum,<br \/>\n                                  printSomething);<br \/>\n  return 0;<br \/>\n}<br \/>\n[\/c]<\/p>\n<p>This worked fine on my gcc compiler (MacOSX 10.5), no need to even use * or &#038; operators when defining the parameters or when passing the functions as parameters.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[c] #include &lt;stdio.h&gt; \/\/Example to show how to pass \/\/functions as parameters in C. \/\/simple function that returns the sum of two ints int sum(int i, int j) { return i+j; } \/\/simple function that prints a char* void printSomething(char* something) { printf(&quot;%sn&quot;,something); } \/\/function that takes \/\/ 2 ints \/\/ 1 char* \/\/ one [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_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},"jetpack_post_was_ever_published":false},"categories":[15],"tags":[],"class_list":["post-2029","post","type-post","status-publish","format-standard","hentry","category-code"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-wJ","jetpack-related-posts":[{"id":799,"url":"https:\/\/www.gubatron.com\/blog\/function-callbacks-in-c\/","url_meta":{"origin":2029,"position":0},"title":"Function callbacks in C","author":"gubatron","date":"May 2, 2008","format":false,"excerpt":"Ever since I started programming in Javascript, and doing asynchronous function calls, I've found myself to be addicted to passing functions as parameters. I do it a lot in python and php, it's very easy to do this on all these dynamic typed languages. I never had this concept of\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1269,"url":"https:\/\/www.gubatron.com\/blog\/how-to-make-a-quick-dirty-hexviewer-updated\/","url_meta":{"origin":2029,"position":1},"title":"How to make a Quick &#038; Dirty HexViewer &#8211; Updated","author":"gubatron","date":"April 20, 2009","format":false,"excerpt":"After I received comments from ispak on Flickr I made a few fixes. ispak pointed out that it was a bad idea reading one byte at the time, also I had a gay ass try\/catch that didn't catch any exception :p So now I read 16 byte chunks, and I\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"HexViewer - r2","src":"https:\/\/i0.wp.com\/farm4.static.flickr.com\/3486\/3458529439_31357e0afe_o.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/farm4.static.flickr.com\/3486\/3458529439_31357e0afe_o.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/farm4.static.flickr.com\/3486\/3458529439_31357e0afe_o.png?resize=525%2C300 1.5x"},"classes":[]},{"id":3386,"url":"https:\/\/www.gubatron.com\/blog\/how-to-make-a-foreach-function-in-javascript\/","url_meta":{"origin":2029,"position":2},"title":"How to make a &#8220;foreach&#8221; function in JavaScript","author":"gubatron","date":"April 4, 2015","format":false,"excerpt":"I thought this would be a simple exercise in case of having to interview someone for a JavaScript position. \"How would you make your own 'foreach' in JavaScript\" I came up with the following solution: \/\/ \/\/ collection: A list of objects. \/\/ onElementIterationCallback: The function to be called on\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2047,"url":"https:\/\/www.gubatron.com\/blog\/map-function-in-java\/","url_meta":{"origin":2029,"position":3},"title":"Map function in Java","author":"gubatron","date":"August 31, 2010","format":false,"excerpt":"I read on some email signature something along the lines of: \"If I had a dollar for every for(int i=0; i < size; i++) { ... } I've written I'd be rich\" After coding on Android and learning about some of the tips for performance, like \"With an ArrayList, a\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3616,"url":"https:\/\/www.gubatron.com\/blog\/codejava-argb_8888-pixel-abstraction\/","url_meta":{"origin":2029,"position":4},"title":"[CODE\/JAVA] ARGB_8888 Pixel Abstraction","author":"gubatron","date":"December 4, 2016","format":false,"excerpt":"This is one of the ways you can decode Pixel colors out of the integers you get from Android Pixels. ARGB_8888, stands for Alpha, Reg, Green, Blue. The 8's stand for the number of bits per channel. In Android, signed int's are used to represent pixel's alpha\/color information. Since Android's\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3469,"url":"https:\/\/www.gubatron.com\/blog\/things-to-think-about-when-tuning-libtorrent-for-high-performance\/","url_meta":{"origin":2029,"position":5},"title":"Things to think about when tuning libtorrent for high performance","author":"gubatron","date":"January 8, 2016","format":false,"excerpt":"An user on the libtorrent mailing list had the following problem: I'm doing some testing for a libtorrent application that will use a small number of peers (often just 1 seed and 1 downloaded) and high bandwidth delay product links (in the hundreds of Mb\/s and 100ms+ round trip). I'm\u2026","rel":"","context":"In &quot;Bittorrent&quot;","block_context":{"text":"Bittorrent","link":"https:\/\/www.gubatron.com\/blog\/category\/bittorrent\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2016\/01\/latency.jpg?fit=1000%2C667&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2016\/01\/latency.jpg?fit=1000%2C667&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2016\/01\/latency.jpg?fit=1000%2C667&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2016\/01\/latency.jpg?fit=1000%2C667&ssl=1&resize=700%2C400 2x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/2029","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=2029"}],"version-history":[{"count":0,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/2029\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=2029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=2029"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=2029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}