<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>pointers to funtions | Student Projects</title>
	<atom:link href="https://studentprojects.in/tag/pointers-to-funtions/feed/" rel="self" type="application/rss+xml" />
	<link>https://studentprojects.in</link>
	<description>Microcontroller projects, Circuit Diagrams, Project Ideas</description>
	<lastBuildDate>Sun, 28 Aug 2011 15:38:38 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.7</generator>
	<item>
		<title>What is function pointer and how to use function pointer?</title>
		<link>https://studentprojects.in/software-development/c-tutorials/c-faq/function-pointer-function-pointer/</link>
					<comments>https://studentprojects.in/software-development/c-tutorials/c-faq/function-pointer-function-pointer/#comments</comments>
		
		<dc:creator><![CDATA[Editorial Team]]></dc:creator>
		<pubDate>Sun, 28 Aug 2011 14:41:07 +0000</pubDate>
				<category><![CDATA[C Questions & Answers]]></category>
		<category><![CDATA[function pointers]]></category>
		<category><![CDATA[pointers to funtions]]></category>
		<category><![CDATA[function pointer advantages]]></category>
		<guid isPermaLink="false">http://studentprojects.in/?p=1736</guid>

					<description><![CDATA[<p>A function pointer is a variable which is used to hold the starting address of a functions and the same can be used to invoke a function. It is also possible to pass address of different functions at different times thus making the function more flexible and abstract. So the function pointers can be used</p>
<p>The post <a href="https://studentprojects.in/software-development/c-tutorials/c-faq/function-pointer-function-pointer/">What is function pointer and how to use function pointer?</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>A function pointer is a variable which is used to hold the starting address of a functions and the same can be used to invoke a function. It is also possible to pass address of different functions at different times thus making the function more flexible and abstract.</p>
<p>So the function pointers can be used to simplify code by providing a simple way to select a function to execute based on run-time values. Function pointers do always point to a function having a specific signature. Thus, all functions used with the same function pointer must have the same parameters and return type</p>
<p><strong>Syntax:</strong></p>
<pre lang="cpp" escaped="true">ReturnType (*PtrToFun)(arguments if any)</pre>
<p><strong>Example:</strong></p>
<pre lang="cpp" escaped="true" line="1">
#include "stdio.h"
void my_int_func(int x)
{
    printf( "%d\n", x );
}

int main()
{
    void (*foo)(int);
    /* the ampersand is actually optional */
    foo = &my_int_func;
    foo(10);
    return 0;
}
</pre>
<p>In above example function pointer foo is used to call the function my_int_func.</p>
<p><strong>Uses of Function Pointers</strong></p>
<p>Pointer to function is useful when you want to choose a function dynamically at run time &#8211; usually based on certain conditions. Function Pointers are used to implement generic functions like qsort(), callbacks, state machines and anywhere else run time binding of a function is required. Also Function pointers are used to store interrupt handlers in tables.</p>
<p>To begin, check out the qsort fucntion</p>
<pre lang="cpp" escaped="true">void qsort(void *base, size_t nmemb, size_t size, 
           int (*compar)(const void *, const void *));</pre>
<p>Here, you need to pass a pointer to the function that compares the objects that you are sorting. Since qsort() is a generic implementation and does not care what you are sorting, it is up to you to implement a funtion which compares the objects that you are sorting and pass it on to qsort(). qsort() just invokes your compare function by dereferencing the function pointer.</p><p>The post <a href="https://studentprojects.in/software-development/c-tutorials/c-faq/function-pointer-function-pointer/">What is function pointer and how to use function pointer?</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/c-tutorials/c-faq/function-pointer-function-pointer/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
