Function pointers to printf

Discussion in 'C Programming' started by Prakru, Apr 20, 2004.

  1. Prakru

    Prakru Guest

    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
     
    Prakru, Apr 20, 2004
    #1
    1. Advertising

  2. Prakru wrote in news: 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.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Apr 20, 2004
    #2
    1. Advertising

  3. 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;
    }
     
    Martin Ambuhl, Apr 20, 2004
    #3
  4. Rob Williscroft wrote to both comp.lang.c and comp.lang.c++:

    > Prakru wrote in news: 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.
     
    Martin Ambuhl, Apr 20, 2004
    #4
  5. (Prakru) wrote in message news:<>...
    > 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.
     
    Michiel Salters, Apr 20, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Prakru

    Function pointers to printf

    Prakru, Apr 20, 2004, in forum: C++
    Replies:
    4
    Views:
    424
    Michiel Salters
    Apr 20, 2004
  2. ben
    Replies:
    4
    Views:
    645
    Martin Ambuhl
    Jun 26, 2004
  3. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,309
  4. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    452
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    714
Loading...

Share This Page