<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1516773263491418468</id><updated>2011-11-27T15:20:18.415-08:00</updated><title type='text'>Technical Topics</title><subtitle type='html'>All Technical topics, latest technology, old C and CPP , etc</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://technicaltopics.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1516773263491418468/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://technicaltopics.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Tech Speak</name><uri>http://www.blogger.com/profile/11935846525665206149</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1516773263491418468.post-1574059445251699854</id><published>2009-06-05T00:59:00.000-07:00</published><updated>2009-06-05T01:01:22.746-07:00</updated><title type='text'>FUnction Pointers in C</title><content type='html'>&lt;p style="color: rgb(0, 0, 102);"&gt;A useful technique is the ability to have pointers to functions. Their    declaration is easy: write the declaration as it would be for the    function, say&lt;/p&gt;    &lt;pre style="color: rgb(0, 0, 102);"&gt;int func(int a, float b);&lt;/pre&gt;    &lt;p style="color: rgb(0, 0, 102);"&gt;and simply put brackets around the name and a &lt;code&gt;*&lt;/code&gt; in front    of it: that declares the pointer. Because of precedence, if you don't    parenthesize the name, you declare a function returning a pointer:&lt;/p&gt;    &lt;pre style="color: rgb(0, 0, 102);"&gt;/* function returning pointer to int */&lt;br /&gt;int *func(int a, float b);&lt;br /&gt;&lt;br /&gt;/* pointer to function returning int */&lt;br /&gt;int (*func)(int a, float b);&lt;/pre&gt;    &lt;p style="color: rgb(0, 0, 102);"&gt;Once you've got the pointer, you can assign the address of the right    sort of function just by using its name: like an array, a function name    is turned into an address when it's used in an expression. You can call    the function using one of two forms:&lt;/p&gt;    &lt;pre style="color: rgb(0, 0, 102);"&gt;(*func)(1,2);&lt;br /&gt;/* or */&lt;br /&gt;func(1,2);&lt;/pre&gt;    &lt;p style="color: rgb(0, 0, 102);"&gt;The second form has been newly blessed by the Standard. Here's a simple    example.&lt;/p&gt;    &lt;div style="color: rgb(0, 0, 102);" class="inset-example"&gt;&lt;a name="example-16"&gt;&lt;/a&gt;&lt;pre&gt;#include &lt;stdio.h&gt;&lt;br /&gt;#include &lt;stdlib.h&gt;&lt;br /&gt;&lt;br /&gt;void func(int);&lt;br /&gt;&lt;br /&gt;main(){&lt;br /&gt;     void (*fp)(int);&lt;br /&gt;&lt;br /&gt;     fp = func;&lt;br /&gt;&lt;br /&gt;     (*fp)(1);&lt;br /&gt;     fp(2);&lt;br /&gt;&lt;br /&gt;     exit(EXIT_SUCCESS);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void&lt;br /&gt;func(int arg){&lt;br /&gt;     printf("%d\n", arg);&lt;br /&gt;}&lt;/pre&gt;&lt;cite&gt;Example 5.16&lt;/cite&gt;&lt;/div&gt;    &lt;p style="color: rgb(0, 0, 102);"&gt;If you like writing finite state machines, you might like to know that    you can have an array of pointers to functions, with declaration and use    like this:&lt;/p&gt;    &lt;a style="color: rgb(0, 0, 102);" name="example-17"&gt;&lt;/a&gt;&lt;pre style="color: rgb(0, 0, 102);"&gt;void (*fparr[])(int, float) = {&lt;br /&gt;                             /* initializers */&lt;br /&gt;                     };&lt;br /&gt;/* then call one */&lt;br /&gt;&lt;br /&gt;fparr[5](1, 3.4);&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1516773263491418468-1574059445251699854?l=technicaltopics.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://technicaltopics.blogspot.com/feeds/1574059445251699854/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1516773263491418468&amp;postID=1574059445251699854' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1516773263491418468/posts/default/1574059445251699854'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1516773263491418468/posts/default/1574059445251699854'/><link rel='alternate' type='text/html' href='http://technicaltopics.blogspot.com/2009/06/function-pointers-in-c.html' title='FUnction Pointers in C'/><author><name>Tech Speak</name><uri>http://www.blogger.com/profile/11935846525665206149</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
