| | |
Parameter lists for fucntion pointers??
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2006
Posts: 21
Reputation:
Solved Threads: 0
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.
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.
No you can't... When you declare 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 or maybe use a default argument for the third argument of f2 to be able to assign both f1 and f2 to fptr.
C Syntax (Toggle Plain Text)
void (*fptr)(const void* , const void*);
Hence, you can only pass f1 as an argument.
You probably have to declare fptr as
C Syntax (Toggle Plain Text)
void (*fptr)(const void* , const void*, ... );
•
•
Join Date: Sep 2006
Posts: 21
Reputation:
Solved Threads: 0
•
•
•
•
You probably have to declare fptr asor maybe use a default argument for the third argument of f2 to be able to assign both f1 and f2 to fptr.C Syntax (Toggle Plain Text)
void (*fptr)(const void* , const void*, ... );
thanks.
•
•
•
•
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...
•
•
•
•
If the above is true, ... then how do i call tht function pointer in the code? how will i pass the paramter list?
Thanks
C Syntax (Toggle Plain Text)
void (*fptr)(const void* , const void*, ... ); 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:
C Syntax (Toggle Plain Text)
fptr( &a,&b,&c); fptr( &a,&b );
•
•
Join Date: May 2004
Posts: 178
Reputation:
Solved Threads: 10
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.
C Syntax (Toggle Plain Text)
#include <string.h> int myfunc(const char *a, const char *b, const int i) { ............. stuff return 1; } void foo(char *a, char *b, int c) { typedef int (*Fx)(); Fx fx[2]={strcmp,myfunc}; int result=0; if (c==1) result=fx[0](a,b); else result=fx[1](a,b,c); if(result==0) printf("okay\n"); }
![]() |
| Thread Tools | Search this Thread |
#include * append array arrays asterisks bash binarysearch calculate changingto char character cm copyimagefile creafecopyofanytypeoffileinc createprocess() database dynamic execv feet fgets file floatingpointvalidation fork forloop framework function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide include incrementoperators input intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix meter microsoft mqqueue number oddnumber odf opensource openwebfoundation overwrite owf pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing scripting segmentationfault sequential single socket socketprogramming standard strchr string systemcall testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






:cheesy: