943,640 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2960
  • C RSS
Dec 12th, 2006
0

Parameter lists for fucntion pointers??

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
caltiger is offline Offline
21 posts
since Sep 2006
Dec 12th, 2006
1

Re: Parameter lists for fucntion pointers??

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.
Reputation Points: 28
Solved Threads: 2
Junior Poster in Training
vicky_dev is offline Offline
86 posts
since May 2005
Dec 12th, 2006
0

Re: Parameter lists for fucntion pointers??

Click to Expand / Collapse  Quote originally posted by vicky_dev ...
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.

Reputation Points: 10
Solved Threads: 0
Newbie Poster
caltiger is offline Offline
21 posts
since Sep 2006
Dec 12th, 2006
0

Re: Parameter lists for fucntion pointers??

If the above is true, ... then how do i call tht function pointer in the code? how will i pass the paramter list?

Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
caltiger is offline Offline
21 posts
since Sep 2006
Dec 12th, 2006
0

Re: Parameter lists for fucntion pointers??

Click to Expand / Collapse  Quote originally posted by caltiger ...
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...
Reputation Points: 28
Solved Threads: 2
Junior Poster in Training
vicky_dev is offline Offline
86 posts
since May 2005
Dec 12th, 2006
0

Re: Parameter lists for fucntion pointers??

Click to Expand / Collapse  Quote originally posted by caltiger ...
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 );
Reputation Points: 28
Solved Threads: 2
Junior Poster in Training
vicky_dev is offline Offline
86 posts
since May 2005
Dec 12th, 2006
1

Re: Parameter lists for fucntion pointers??

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. }
Reputation Points: 62
Solved Threads: 10
Junior Poster
jim mcnamara is offline Offline
179 posts
since May 2004
Dec 13th, 2006
0

Thanx!!!

hi guys,
i got it.. ur input was really helpful.

thanks again...
:cheesy:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
caltiger is offline Offline
21 posts
since Sep 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: question
Next Thread in C Forum Timeline: help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC