{"id":799,"date":"2008-05-02T12:46:27","date_gmt":"2008-05-02T19:46:27","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/?p=799"},"modified":"2008-05-02T12:46:27","modified_gmt":"2008-05-02T19:46:27","slug":"function-callbacks-in-c","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/function-callbacks-in-c\/","title":{"rendered":"Function callbacks in C"},"content":{"rendered":"<p>Ever since I started programming in Javascript, and doing asynchronous function calls, I&#8217;ve found myself to be addicted to passing functions as parameters.<\/p>\n<p>I do it a lot in python and php, it&#8217;s very easy to do this on all these dynamic typed languages.<\/p>\n<p>I never had this concept of passing functions as parameters, or pointers to functions as parameters when I was a kid in school and we were doing stuff in C or Pascal, I&#8217;d deal with it with ifs and switches.<\/p>\n<p>So, this afternoon I decided to read a little bit and give it a try in C.<\/p>\n<p>Here&#8217;s some code for future reference If I ever need it, it&#8217;s pretty easy.<\/p>\n<pre>\n#include <stdio.h>\nvoid this() { printf(\"Thisn\"); }\nvoid that() { printf(\"Thatn\"); }\n\nint sum(int x, int y) {\treturn x+y; }\n\nint mul(int x, int y) { return x*y; }\n\n\/\/Function that takes a callback that uses no parameters\nvoid callanother(void (*callback)()) {\n  (*callback)();\n}\n\n\/\/Function that takes a callback that\n\/\/takes 2 int parameters and returns int\nint callComplexCallback(int (*callback)(),int a, int b) {\n  return (*callback)(a,b);\n}\n\nint main (int argc, char** argv) {\n  callanother(this);\n  callanother(that);\n\n  printf(\"n\");\n\n  int w = 20;\n  int h = 30;\n\n  printf(\"%dn\",callComplexCallback(sum,w,h));\n  printf(\"%dn\",callComplexCallback(mul,w,h));\n\n  \/\/this also works\n  printf(\"%dn\",callComplexCallback((*sum),w,h));\n  printf(\"%dn\",callComplexCallback((*mul),w,h));\n\n  return 0;\n}\n<\/pre>\n<p>The output is this:<\/p>\n<pre>\n~$ .\/a.out \nThis\nThat\n\n50\n600\n50\n600\n<\/pre>\n<p>The whole trick is how you define the function that will take the other function as a parameter.<\/p>\n<p>If you have a function:<\/p>\n<pre>\nvoid whatever();\n<\/pre>\n<p>The function that&#8217;s supposed to use &#8220;whatever()&#8221; like-functions should look:<\/p>\n<pre>\nvoid useWhateverLikeFunctions(void (*f)()) {\n  ...\n  (*f)();\n}\n<\/pre>\n<p>If you have a callback function that needs parameters, then you define the caller as:<\/p>\n<pre>\nvoid callerFunction(void (*f),int paramA, int* paramB, char paramC) {\n  ...\n  (*f)(paramA,paramB,paramC);\n}\n<\/pre>\n<p>Then you&#8217;d use the function<\/p>\n<pre>\nvoid someCallback(int a, int* b, char c);\n\n...\ncallerFunction(someCallback,a,b,c);\n...\n<\/pre>\n<p>I know this is the oldest thing in the world to C programmers, but it never crossed my mind before, so here it is for my own personal reference, I hope it serves others.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ever since I started programming in Javascript, and doing asynchronous function calls, I&#8217;ve found myself to be addicted to passing functions as parameters. I do it a lot in python and php, it&#8217;s very easy to do this on all these dynamic typed languages. I never had this concept of passing functions as parameters, or [&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,30],"tags":[239,1437,457],"class_list":["post-799","post","type-post","status-publish","format-standard","hentry","category-code","category-geeklife","tag-c","tag-code","tag-function-pointers"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-cT","jetpack-related-posts":[{"id":2029,"url":"https:\/\/www.gubatron.com\/blog\/reference-passing-functions-as-parameters-in-c\/","url_meta":{"origin":799,"position":0},"title":"Reference: Passing functions as parameters in C","author":"gubatron","date":"July 18, 2010","format":false,"excerpt":"[c] #include <stdio.h> \/\/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(\"%sn\",something); } \/\/function that takes \/\/ 2 ints\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":799,"position":1},"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":3386,"url":"https:\/\/www.gubatron.com\/blog\/how-to-make-a-foreach-function-in-javascript\/","url_meta":{"origin":799,"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":3469,"url":"https:\/\/www.gubatron.com\/blog\/things-to-think-about-when-tuning-libtorrent-for-high-performance\/","url_meta":{"origin":799,"position":3},"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":[]},{"id":156,"url":"https:\/\/www.gubatron.com\/blog\/portscanner-only-60-lines-of-code\/","url_meta":{"origin":799,"position":4},"title":"PortScanner &#8211; Only 60 lines of code","author":"gubatron","date":"May 19, 2005","format":false,"excerpt":"Hi, here's a simple port scanner I wrote today in Java. I needed to see something on my server, perhaps you can find it useful. I would say its pretty fast, you can tweak the number of threads and timeouts to adjust to your server\/connection. I did't do much testing\u2026","rel":"","context":"In &quot;Gubatron&quot;","block_context":{"text":"Gubatron","link":"https:\/\/www.gubatron.com\/blog\/category\/gubatron\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2887,"url":"https:\/\/www.gubatron.com\/blog\/android-changing-textview-alpha-transparency-across-different-target-sdks\/","url_meta":{"origin":799,"position":5},"title":"Android: Changing TextView alpha transparency across different target SDKs","author":"gubatron","date":"November 30, 2012","format":false,"excerpt":"Sometimes you may need to make a TextView (label) look a little transparent to make emphasis on other parts of your UI. The .setAlpha() function on TextView is not supported after later in the SDK. Here's a static workaround you can place on some sort of UIUtils class you may\u2026","rel":"","context":"In \"Android\"","block_context":{"text":"Android","link":"https:\/\/www.gubatron.com\/blog\/tag\/android\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/799","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=799"}],"version-history":[{"count":0,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/799\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=799"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}