Prakru wrote to both comp.lang.c and comp.lang.c++:
[My response is in the context of C. Since C++ people may have a
different view, I have set follow-ups to comp.lang.c only. Very few
questions actually belong in both newsgroups, since these are different
languages.]
Can we have a function pointer to printf or any function with variable arguments?
Yes.
I have tried in Microsoft Visual Studio C++ compiler
but could not compile.
Because you're not doing what you think you are.
Is this a compiler dependent problem.
No. Your errors are errors for all C compilers.
If it can't be compiled what are the reasons for it
See below your code ...
typedef int (*printf_ptr) (char *str, ...);
int my_printf (char *str, ...)
{
/* and the standard code for accessing var. args using
* va_args, va_start , va_end */
}
int main ()
{
int i = 10;
printf_ptr = printf;
printf_ptr (" i valus is %d",i);
printf_ptr = my_printf;
printf_ptr (" i valus is %d",i);
}
You are doing several things wrong. You are using a type (printf_ptr)
as if it were a variable (never declared, either). You are using a
signature for your my_printf and printf_ptr incompatible with that for
printf. Compare your code to the following:
#include <stdio.h>
typedef int (*printf_ptr) (const char *str, ...);
int my_printf(const char *str, ...)
{
(void *) str;
return 0;
}
int main()
{
int i = 10;
printf_ptr function;
function = printf;
function(" i valus is %d", i);
function = my_printf;
function(" i valus is %d", i);
return 0;
}