transfering variable length argument list

Discussion in 'C++' started by ANDERS FLODERUS, Dec 20, 2003.

  1. Is it possible to transfer a variable length argument
    list? What I want to do is just

    void myPrintf( char *pFormat, ... ) {
    char buf[256];
    sprintf( buf, pFormat, ... );
     
    ANDERS FLODERUS, Dec 20, 2003
    #1
    1. Advertising

  2. ANDERS FLODERUS wrote in news:1pTEb.253$:

    > Is it possible to transfer a variable length argument
    > list? What I want to do is just
    >
    > void myPrintf( char *pFormat, ... ) {
    > char buf[256];
    > sprintf( buf, pFormat, ... );
    > .
    > .
    > .
    > return;
    > }
    >
    > which obviously will not compile. If possible, I would
    > like to do it without using the va_... macros while stepping
    > throu the format specifier. I suppose it would be possible
    > althou I do not know what type the arguments are (could
    > be found in the format specifier?).
    >


    #include <cstdio>
    #include <cstdarg>

    void myPrintf( char const *pFormat, ... )
    {
    using namespace std;

    char buf[256];

    va_list va;
    va_start( va, pFormat );

    vsprintf( buf, pFormat, va );


    /* _vsnprintf is non standard but your compiler may provide it.
    If so its *safer* than the above.
    */
    //_vsnprintf( buf, sizeof buf, pFormat, va );


    va_end( va );

    printf( "buf = [%s]\n", buf );
    }

    int main()
    {
    myPrintf("test %d", 1 );
    }

    HTH.

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Dec 20, 2003
    #2
    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. Replies:
    3
    Views:
    464
  2. Replies:
    3
    Views:
    623
  3. Ben Kial
    Replies:
    1
    Views:
    664
    Eric Enright
    Nov 15, 2004
  4. Joakim Hove
    Replies:
    5
    Views:
    519
    Joakim Hove
    Mar 14, 2008
  5. Keve Nagy
    Replies:
    4
    Views:
    2,101
    Keve Nagy
    Apr 4, 2009
Loading...

Share This Page