User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 375,171 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,168 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 1314 | Replies: 5
Reply
Join Date: Jul 2006
Posts: 31
Reputation: joshilay is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
joshilay joshilay is offline Offline
Light Poster

Help what is pointer to function ???

  #1  
Jul 4th, 2006
can anyone tell me what are pointers to function ??? and how are they implemented ??
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2005
Posts: 74
Reputation: CrazyDieter is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
CrazyDieter's Avatar
CrazyDieter CrazyDieter is offline Offline
Junior Poster in Training

Re: what is pointer to function ???

  #2  
Jul 4th, 2006
Originally Posted by joshilay
can anyone tell me what are pointers to function ??? and how are they implemented ??


in C, functions are defined just like variable.

The adress of a function can be assimiled to its adress in memory, so the following code is valid :
#include <stdio.h>


void something()
{
    printf("hello\n");
}


void somethingElse()
{
    printf("hello2\n");
}

int main(void)
{
   (void)(*pointer)() = something;

   pointer();
   
   pointer = somethingElse;

   pointer();

    return 0;
}
Last edited by CrazyDieter : Jul 4th, 2006 at 2:27 pm.
Reply With Quote  
Join Date: Jun 2006
Posts: 81
Reputation: Laiq Ahmed is on a distinguished road 
Rep Power: 3
Solved Threads: 8
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster in Training

Re: what is pointer to function ???

  #3  
Jul 5th, 2006
Pointer to function are the pointers which have the ability to call the function pointed to by them.As the ordinary pointers take the address of variable and can change the value pointed by them, the function pointer almost does the same.

 
 
#include <iostream>
#include <string>
 
using std::string;
using std::cout;
 
//simple function
void print(string _name) { std::cout<<"Hello"<<_name; }
 
//another function with return type int.
int cal_pay(double _pay,int _hours) 
{
    return (2*_pay + 1000)*_hours;
}
 
//function pointer of the same type i.e. complete function type must match exactly
void (*pfct)(string);   //check the return type and arguments   
int (*cfct)(double,int);
int main() 
{
     pfct=&print;             //pfct points to print function
     pfct("Laiq");
     
     cfct=cal_pay            //also o.k. same as &cal_pay
     int ans = (*cfct)(500.00,4);    //another way of calling 
     return 0;
}

you can use typedef to clean declaration
typedef void (*pfct)(string);
void say(string);
void no(string);
void yes(string);
pfct my[] = { &say, &no, &yes };

also you can use the pointer to function in function's argument to specifify the details... like comparision criteria in sort funciton ;

typedef int (*Comfct)(MyType const*, MyType const*);
void sort(vector<MyType>& v,size_t n, Comfct Cmp);

Hope you got the basic idea..............
Reply With Quote  
Join Date: Jun 2005
Posts: 13
Reputation: thandermax is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
thandermax thandermax is offline Offline
Newbie Poster

Re: what is pointer to function ???

  #4  
Jul 11th, 2006
Mainly used in TSR programming in old days , where old IVT vector function 's address is stored as a pointer to a function.
Reply With Quote  
Join Date: Apr 2004
Posts: 3,422
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 15
Solved Threads: 137
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: what is pointer to function ???

  #5  
Jul 11th, 2006
Reply With Quote  
Join Date: Jun 2006
Posts: 81
Reputation: Laiq Ahmed is on a distinguished road 
Rep Power: 3
Solved Threads: 8
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster in Training

Re: what is pointer to function ???

  #6  
Jul 16th, 2006
Amazing Article . Thanks from my side .
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

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

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 11:55 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC