{"id":135,"date":"2009-07-24T12:11:41","date_gmt":"2009-07-24T11:11:41","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=135"},"modified":"2009-07-24T12:11:41","modified_gmt":"2009-07-24T11:11:41","slug":"function-pointers","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2009\/07\/24\/function-pointers\/","title":{"rendered":"Function Pointers"},"content":{"rendered":"<p>Function pointers are just like any other type of pointer within C\/C++, in that they point to a reference in memory to something that is the reference to the object (a pointer to a integer type that is holding the value 2 is the value of 2 and the pointer just points to this object).  Since a pointer can point to any type of object that is similar to the object type, you can also have pointers to functions.  This type of pointer is good for having a single function pointer to list of functions that is chosen from a action, for example if there was a list of function for the edit action within the standard document editors (Cut\/Copy\/Paste\/Undo\/Redo) and to just point to within every function was required was to use the drop down list item number, then the code is very small and also re-usable.<\/p>\n<p>For example of code with using function pointers.<\/p>\n<pre lang=\"cpp\">\r\n#include <iostream>\r\n\r\nusing namespace std;\r\n\r\nvoid f()\r\n{\r\n       cout << \"hi\" << endl;       \r\n}\r\n\r\nvoid g()\r\n{\r\n       cout << \"bye\" << endl;\r\n}\r\n\r\n\/\/ always set the function pointer to NULL.\r\nvoid (*func)() = NULL;\r\n\r\n\/\/ to pass a function pointer to a function.\r\nvoid callFunc( void (*function)())\r\n{\r\n       function();\r\n}\r\n\r\nint main(int argc, char* argv[])\r\n{\r\n       \/\/ set the func object (as created above \u00e2\u20ac\u201c void (*func)() = NULL; - to the function address f)\r\n       func=&f;\r\n       \/\/ call the function that is assoicated with the func pointer\r\n       func();\r\n       \/\/ reset to another function.\r\n       func=&g;\r\n       func();\r\n\r\n       \/\/ call the function pointed to within another function.\r\n       callFunc(func);\r\n       return 0;\r\n}\r\n<\/pre>\n<p>I have included Microsoft Visual Studio 2005 downloadable edition (<a title=\"here\"  href=\"http:\/\/msdn.microsoft.com\/vstudio\/express\/default.aspx\" target=\"_blank\">here<\/a>) and also Linux code for this tutorial as a download.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Function pointers are just like any other type of pointer within C\/C++, in that they point to a reference in memory to something that is the reference to the object (a pointer to a integer type that is holding the value 2 is the value of 2 and the pointer just points to this object). &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2009\/07\/24\/function-pointers\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Function Pointers<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[10,9,8,398,400],"class_list":["post-135","post","type-post","status-publish","format-standard","hentry","category-c_and_cpp","tag-c","tag-gcc","tag-gnu","tag-linux","tag-windows"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=135"}],"version-history":[{"count":1,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/135\/revisions"}],"predecessor-version":[{"id":136,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/135\/revisions\/136"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}