what is pointer to function ???

Reply

Join Date: Jul 2006
Posts: 31
Reputation: joshilay is an unknown quantity at this point 
Solved Threads: 0
joshilay joshilay is offline Offline
Light Poster

what is pointer to function ???

 
0
  #1
Jul 4th, 2006
can anyone tell me what are pointers to function ??? and how are they implemented ??
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 76
Reputation: CrazyDieter is an unknown quantity at this point 
Solved Threads: 3
CrazyDieter's Avatar
CrazyDieter CrazyDieter is offline Offline
Junior Poster in Training

Re: what is pointer to function ???

 
0
  #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 :
  1. #include <stdio.h>
  2.  
  3.  
  4. void something()
  5. {
  6. printf("hello\n");
  7. }
  8.  
  9.  
  10. void somethingElse()
  11. {
  12. printf("hello2\n");
  13. }
  14.  
  15. int main(void)
  16. {
  17. (void)(*pointer)() = something;
  18.  
  19. pointer();
  20.  
  21. pointer = somethingElse;
  22.  
  23. pointer();
  24.  
  25. return 0;
  26. }
Last edited by CrazyDieter; Jul 4th, 2006 at 3:27 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: what is pointer to function ???

 
0
  #3
Jul 6th, 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.

  1.  
  2.  
  3. #include <iostream>
  4. #include <string>
  5.  
  6. using std::string;
  7. using std::cout;
  8.  
  9. //simple function
  10. void print(string _name) { std::cout<<"Hello"<<_name; }
  11.  
  12. //another function with return type int.
  13. int cal_pay(double _pay,int _hours)
  14. {
  15. return (2*_pay + 1000)*_hours;
  16. }
  17.  
  18. //function pointer of the same type i.e. complete function type must match exactly
  19. void (*pfct)(string); //check the return type and arguments
  20. int (*cfct)(double,int);
  21. int main()
  22. {
  23. pfct=&print; //pfct points to print function
  24. pfct("Laiq");
  25.  
  26. cfct=cal_pay //also o.k. same as &cal_pay
  27. int ans = (*cfct)(500.00,4); //another way of calling
  28. return 0;
  29. }

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 Quick reply to this message  
Join Date: Jun 2005
Posts: 14
Reputation: thandermax is an unknown quantity at this point 
Solved Threads: 0
thandermax thandermax is offline Offline
Newbie Poster

Re: what is pointer to function ???

 
0
  #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 Quick reply to this message  
Join Date: Apr 2004
Posts: 4,361
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 240
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: what is pointer to function ???

 
0
  #5
Jul 11th, 2006
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: what is pointer to function ???

 
0
  #6
Jul 16th, 2006
Amazing Article . Thanks from my side .
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC