Function pointers to printf

P

Prakru

Hello,

Can we have a function pointer to printf or any function with variable arguments?


I have tried in Microsoft Visual Studio C++ compiler
but could not compile.

Is this a compiler dependent problem.
If it can't be compiled what are the reasons for it



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


Thanks
Prakru
 
R

Rob Williscroft

Prakru wrote in in
comp.lang.c++:
Hello,

Can we have a function pointer to printf or any function with variable
arguments?


I have tried in Microsoft Visual Studio C++ compiler
but could not compile.

Is this a compiler dependent problem.
If it can't be compiled what are the reasons for it



typedef int (*printf_ptr) (char *str, ...);

The Standard conforming declaration for std::printf is

int printf( char const *, ... ); /* In namespace std of course */

So your typedef needs to be:

typedef int (*printf_ptr) (char const *str, ...);
int my_printf (char *str, ...)

int my_printf( char const *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 is a *type*, you need to create in instance of this type, do:

printf_ptr p = std::printf

[snip]


#include <cstdio>

typedef int (*printf_ptr) (char const *str, ...);

int main ()
{
int i = 10;
printf_ptr p = std::printf;

p( "i value is %d\n", i );
}

HTH.

Rob.
 
M

Martin Ambuhl

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;
}
 
M

Martin Ambuhl

Rob Williscroft wrote to both comp.lang.c and comp.lang.c++:
Prakru wrote in in
comp.lang.c++:

And comp.lang.c as well.
Since you don't know where Prakru is reading this, it is reasonable to
respond in both newsgroups but ...
The Standard conforming declaration for std::printf is

And similar things suggest that you should set follow-ups to
comp.lang.c++ only, as I have done with this post.
 
M

Michiel Salters

Hello,

Can we have a function pointer to printf or any function
with variable arguments?

Yes. Make sure the complete argument list matches the
declaration of the printf functions.
typedef int (*printf_ptr) (char *str, ...);

printf doesn't try to modify its first argument. Therefore,
the first argument is actually const char*. You can't
assign &printf to printf_ptr.
int my_printf (char *str, ...)
{
/* and the standard code for accessing var. args using
* va_args, va_start , va_end */
}

Are you going to modify str? Then why do you want write access?

BTW, next time, read the error message. If you don't undertand it,
please post it as well so we can explain the error message.

Regards,
Michiel Salters.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top