Creating a va_list

Discussion in 'C Programming' started by P. Hari Krishna, Sep 8, 2004.

  1. Hi,
    I have a situation where I do not know the number of arguments I pass to a
    function at compile time.
    So is there any way, that we can programmatically create a "va_list" and then
    pass it to the function that can the use to do some sprintfs ?

    Thanks
    -Hari
    P. Hari Krishna, Sep 8, 2004
    #1
    1. Advertising

  2. P. Hari Krishna

    Eric Sosman Guest

    P. Hari Krishna wrote:
    > Hi,
    > I have a situation where I do not know the number of arguments I pass to a
    > function at compile time.
    > So is there any way, that we can programmatically create a "va_list" and then
    > pass it to the function that can the use to do some sprintfs ?


    No; there is no portable way to construct an argument
    list at run-time. Every function call has a fixed number
    of arguments.

    I'm not sure what you're trying to do, but perhaps
    the vsprintf() function might be of use.

    --
    Eric Sosman, Sep 8, 2004
    #2
    1. Advertising

  3. "P. Hari Krishna" <> wrote in message
    news:p...
    > Hi,
    > I have a situation where I do not know the number of arguments I pass to a
    > function at compile time.
    > So is there any way, that we can programmatically create a "va_list" and
    > then
    > pass it to the function that can the use to do some sprintfs ?


    Unless I've completely misunderstood your question, surely it'd make sense
    to just use one argument - a pointer to a NULL terminated array of (possibly
    void) pointers? -- or two arguments if you're not sure at compiletime the
    type of the data you're passing to your function. Or is the problem that you
    need to pass an unknown number of arguments to a varargs function that has
    been defined by someone other than you? -- if this is the case, I don't
    believe it's possible in standard C, although it may be possible with some
    machine-specific ASM.


    ~Kieran Simkin
    Digital Crocus
    http://digital-crocus.com/
    Kieran Simkin, Sep 8, 2004
    #3
  4. P. Hari Krishna

    Moonie Guest

    void function( char *ptr, ... )
    {
    va_list args;
    char format[81];

    va_start( args, ptr );
    vssprintf( format, ptr, args );
    va_end( args );
    }

    the moral of the story is that ptr must be in a format for sprintf
    for example: "%d%c%s" etc. and format must be the length of the string
    that you expect.

    Marc Smith.....Moonie



    --
    Moonie
    ------------------------------------------------------------------------
    Posted via http://www.codecomments.com
    ------------------------------------------------------------------------
    Moonie, Sep 9, 2004
    #4
  5. P. Hari Krishna

    Moonie Guest

    vssprint should be vsprintf

    so sorry about that.

    Marc Smith.....Moonie



    --
    Moonie
    ------------------------------------------------------------------------
    Posted via http://www.codecomments.com
    ------------------------------------------------------------------------
    Moonie, Sep 9, 2004
    #5
  6. On Wed, 08 Sep 2004 10:29:17 -0400, Eric Sosman wrote:

    > P. Hari Krishna wrote:
    >> Hi,
    >> I have a situation where I do not know the number of arguments I pass to a
    >> function at compile time.
    >> So is there any way, that we can programmatically create a "va_list" and then
    >> pass it to the function that can the use to do some sprintfs ?

    >
    > No; there is no portable way to construct an argument
    > list at run-time. Every function call has a fixed number
    > of arguments.
    >
    > I'm not sure what you're trying to do, but perhaps
    > the vsprintf() function might be of use.

    The arguments are in the form of a list (or an array of pointers to
    strings)and so I cannot know the number of arguments at compile time.
    But I think I have got the solution (portable). All I have in the
    formatting string is only a bunch of %s, so I wrote a small sprintf-like
    function that will go and parse the formatting string for %s and form
    the resulting buffer, manually.

    Thanks anyway.
    -Hari
    P. Hari Krishna, Sep 9, 2004
    #6
    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. va_list in JNI

    , Jun 23, 2005, in forum: Java
    Replies:
    4
    Views:
    2,320
  2. Rich Herrick

    reference to va_list

    Rich Herrick, Jan 16, 2005, in forum: C++
    Replies:
    0
    Views:
    431
    Rich Herrick
    Jan 16, 2005
  3. Peter

    va_list help, please ...

    Peter, Feb 15, 2005, in forum: C++
    Replies:
    6
    Views:
    3,536
    Pete Becker
    Feb 15, 2005
  4. John Guo

    va_list help

    John Guo, Mar 31, 2005, in forum: C++
    Replies:
    5
    Views:
    3,308
    Pete Becker
    Mar 31, 2005
  5. Douwe
    Replies:
    3
    Views:
    734
    Chris Torek
    Aug 30, 2003
Loading...

Share This Page