Function pointers to printf

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Prakru

    #1

    Function pointers to printf

    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
  • Rob Williscroft

    #2
    Re: Function pointers to printf

    Prakru wrote in news:cdb527e9.0 404192241.3dfd5 54a@posting.goo gle.com in
    comp.lang.c++:
    [color=blue]
    > 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, ...);[/color]

    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, ...);
    [color=blue]
    >
    > int my_printf (char *str, ...)[/color]

    int my_printf( char const *str, ... )
    [color=blue]
    > {
    > /* and the standard code for accessing var. args using
    > * va_args, va_start , va_end */
    > }
    >
    > int main ()
    > {
    > int i = 10;
    > printf_ptr = printf;[/color]

    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.
    --

    Comment

    • Martin Ambuhl

      #3
      Re: Function pointers to printf

      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.]
      [color=blue]
      > Can we have a function pointer to printf or any function with variable arguments?[/color]

      Yes.
      [color=blue]
      > I have tried in Microsoft Visual Studio C++ compiler
      > but could not compile.[/color]

      Because you're not doing what you think you are.
      [color=blue]
      > Is this a compiler dependent problem.[/color]

      No. Your errors are errors for all C compilers.
      [color=blue]
      > If it can't be compiled what are the reasons for it[/color]

      See below your code ...
      [color=blue]
      > 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);
      > }
      >[/color]

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

      Comment

      • Martin Ambuhl

        #4
        Re: Function pointers to printf

        Rob Williscroft wrote to both comp.lang.c and comp.lang.c++:
        [color=blue]
        > Prakru wrote in news:cdb527e9.0 404192241.3dfd5 54a@posting.goo gle.com in
        > comp.lang.c++:[/color]

        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 ...
        [color=blue]
        > The Standard conforming declaration for std::printf is[/color]

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

        Comment

        • Michiel Salters

          #5
          Re: Function pointers to printf

          prakrunath@yaho o.com (Prakru) wrote in message news:<cdb527e9. 0404192241.3dfd 554a@posting.go ogle.com>...[color=blue]
          > Hello,
          >
          > Can we have a function pointer to printf or any function
          > with variable arguments?[/color]

          Yes. Make sure the complete argument list matches the
          declaration of the printf functions.
          [color=blue]
          > typedef int (*printf_ptr) (char *str, ...);[/color]

          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.
          [color=blue]
          > int my_printf (char *str, ...)
          > {
          > /* and the standard code for accessing var. args using
          > * va_args, va_start , va_end */
          > }[/color]

          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.

          Comment

          Working...