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.

No you can't... When you declare

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

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.

You probably have to declare fptr as

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.

:D

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

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.

:D

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

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

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:

fptr( &a,&b,&c);
fptr( &a,&b );

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.

#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");
}

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

thanks again...
:):cheesy:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.