Parameter lists for fucntion pointers??

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2006
Posts: 21
Reputation: caltiger is an unknown quantity at this point 
Solved Threads: 0
caltiger caltiger is offline Offline
Newbie Poster

Parameter lists for fucntion pointers??

 
0
  #1
Dec 12th, 2006
hi,
I wanted to know if I can pass functions with varying no. of arguments to a fn pointer. say for example, i define a fn pointer as

void (*fptr)(const void* , const void*);
If there are another pair of fucntions defined as :

void f1(int*, int* );
void f2(int*, int*, int*);

now, can i pass the addresses of both these functions to the function pointer.

fptr = &f1;
fptr = &f2;

:rolleyes::rolleyes::rolleyes:

Thanks.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 86
Reputation: vicky_dev is an unknown quantity at this point 
Solved Threads: 2
vicky_dev's Avatar
vicky_dev vicky_dev is offline Offline
Junior Poster in Training

Re: Parameter lists for fucntion pointers??

 
1
  #2
Dec 12th, 2006
No you can't... When you declare
  1. void (*fptr)(const void* , const void*);
it explicitly means that the function pointer can point only to the functions which take two pointers as arguments and return nothing.
Hence, you can only pass f1 as an argument.

You probably have to declare fptr as
  1. void (*fptr)(const void* , const void*, ... );
or maybe use a default argument for the third argument of f2 to be able to assign both f1 and f2 to fptr.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 21
Reputation: caltiger is an unknown quantity at this point 
Solved Threads: 0
caltiger caltiger is offline Offline
Newbie Poster

Re: Parameter lists for fucntion pointers??

 
0
  #3
Dec 12th, 2006
Originally Posted by vicky_dev View Post
You probably have to declare fptr as
  1. void (*fptr)(const void* , const void*, ... );
or maybe use a default argument for the third argument of f2 to be able to assign both f1 and f2 to fptr.
soooo.. if i define the function pointer as above, i can have more than two parameters in the parameter list???? tht wud be a case of variable argument list right??? :rolleyes:

thanks.

Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 21
Reputation: caltiger is an unknown quantity at this point 
Solved Threads: 0
caltiger caltiger is offline Offline
Newbie Poster

Re: Parameter lists for fucntion pointers??

 
0
  #4
Dec 12th, 2006
If the above is true, ... then how do i call tht function pointer in the code? how will i pass the paramter list?

Thanks
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 86
Reputation: vicky_dev is an unknown quantity at this point 
Solved Threads: 2
vicky_dev's Avatar
vicky_dev vicky_dev is offline Offline
Junior Poster in Training

Re: Parameter lists for fucntion pointers??

 
0
  #5
Dec 12th, 2006
Originally Posted by caltiger View Post
soooo.. if i define the function pointer as above, i can have more than two parameters in the parameter list???? tht wud be a case of variable argument list right??? :rolleyes:

thanks.


Yes, that would be a function taking variable number of arguments... And you can more than two arguments, as long as the first two arguments match the prototype. The remaining parameters can be of any type...
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 86
Reputation: vicky_dev is an unknown quantity at this point 
Solved Threads: 2
vicky_dev's Avatar
vicky_dev vicky_dev is offline Offline
Junior Poster in Training

Re: Parameter lists for fucntion pointers??

 
0
  #6
Dec 12th, 2006
Originally Posted by caltiger View Post
If the above is true, ... then how do i call tht function pointer in the code? how will i pass the paramter list?

Thanks
Actually both the function pointer and the function must have the same prototype,

  1.  
  2. void (*fptr)(const void* , const void*, ... );
  3.  
  4. void f1(int*, int*, ... );

Only then you can assign a funcition to a function pointer. fptr = f2

The parameters can be passed as usual, execpt that any number of them can be passed:
  1. fptr( &a,&b,&c);
  2. fptr( &a,&b );
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 178
Reputation: jim mcnamara is on a distinguished road 
Solved Threads: 10
jim mcnamara jim mcnamara is offline Offline
Junior Poster

Re: Parameter lists for fucntion pointers??

 
1
  #7
Dec 12th, 2006
This is allowable C, but can cause you problems if you are not super careful - meaning: it will cause problems. But it does answer your question.
  1. #include <string.h>
  2. int myfunc(const char *a, const char *b, const int i)
  3. {
  4. ............. stuff
  5. return 1;
  6. }
  7.  
  8. void foo(char *a, char *b, int c)
  9. {
  10. typedef int (*Fx)();
  11. Fx fx[2]={strcmp,myfunc};
  12. int result=0;
  13.  
  14. if (c==1)
  15. result=fx[0](a,b);
  16. else
  17. result=fx[1](a,b,c);
  18. if(result==0)
  19. printf("okay\n");
  20. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 21
Reputation: caltiger is an unknown quantity at this point 
Solved Threads: 0
caltiger caltiger is offline Offline
Newbie Poster

Thanx!!!

 
0
  #8
Dec 13th, 2006
hi guys,
i got it.. ur input was really helpful.

thanks again...
:cheesy:
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC