45,000 Jobs - Get an Interview Call,  Post Your Resume Here
SURESHKUMAR.NET FORUMS
Registered Member Login:
Not a member? Register today!



Welcome to the SURESHKUMAR.NET FORUMS.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.




ds. c , c++ Interview / Technical Questions Kindly solve an many as questions you can. It will sharpen your skills and those solutions will help others too.

Function pointers- simple example

        

Reply
 
LinkBack Thread Tools Display Modes
Old 30-03-07, 01:55 PM   #1 (permalink)
Senior Member
 
Join Date: Oct 2006
Location: In Bangalore
Age: 25
Posts: 126
Thanks: 1
Thanked 9 Times in 9 Posts
Rep Power: 0 jithendra.b will become famous soon enough jithendra.b will become famous soon enough
Function pointers- simple example

Let's discuss briefly about Function pointers...

In C we know that each and every variable has got address. Similarly each and every function has an address associated with it. In order to know the address of the function
we use the following statement..

printf("Address of the function = %u",func); //func is some function.

So it is obvious that we can obtain the address of a function by specifying its name.

Now observe the statement below..

ptr = func;

Here func is the address of the function func. This address is being assigned to ptr.
Now the point is how do we declare ptr..
we declare it as...
(*ptr)();
Since ptr contains address it must be a pointer. In this case the pointer is containing the
address of the function so the declaration goes as (*ptr)() which says ptr is a pointer to a
function which does not have any arguments...

We will put all this discussion to work by considering an example...

#include<stdio.h>
int main()
{
int fun(int,int);
int (*ptr)(int,int);
printf("The address of the function is %u\n",fun);
ptr = fun;
(*ptr)(2,3);
}

int fun(int a,int b)
{
int res;
res = a+b;
printf("%d",res);
}

output: 5

Explanation : Here int (*ptr)(int,int) indicates that ptr is a pointer to a function which takes
two arguments and returns an integer. We can call the function fun by using the address of it. Thus a call is made to it by the statement (*ptr)(2,3)... The rest is
simple.

Advantage of Function pointers : The primary advantage with function pointer is that a function can be called at run time instead of compile time.

jithendra.b is offline Offline   Reply With Quote
Old 30-03-07, 03:31 PM   #2 (permalink)
Junior Member
 
sanjana.btech's Avatar
 
Join Date: Mar 2007
Posts: 19
Thanks: 10
Thanked 14 Times in 2 Posts
Rep Power: 4 sanjana.btech has a spectacular aura about sanjana.btech has a spectacular aura about
Re: Function pointers- simple example

Thank you jithendra....
sanjana.btech is offline Offline   Reply With Quote
Reply

Tags
function , pointers , simple


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
C questions Spoorthi DATA STRUCTURES, C, C++, VC ++ 9 05-11-09 02:18 PM
Mega Free Download S/W Links A-Z yathish OTHERS 516 21-03-09 10:06 AM
TCS Pacement paper 29 sridhar TCS Placement Papers 0 12-02-07 05:43 PM
simple vs real frd Spoorthi JOKES , PRANKS & QUOTES 10 15-12-06 09:44 PM
What’s the difference between thesr shilpa VB / ASP.NET Technologies 5 08-03-06 12:09 PM


All times are GMT +6.5. The time now is 01:12 AM.

More Interview Questions Here...

Content Relevant URLs by vBSEO 3.3.0