Creating a va_list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • P. Hari Krishna

    #1

    Creating a va_list

    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 programmaticall y create a "va_list" and then
    pass it to the function that can the use to do some sprintfs ?

    Thanks
    -Hari


  • Eric Sosman

    #2
    Re: Creating a va_list

    P. Hari Krishna wrote:[color=blue]
    > 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 programmaticall y create a "va_list" and then
    > pass it to the function that can the use to do some sprintfs ?[/color]

    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@sun .com

    Comment

    • Kieran Simkin

      #3
      Re: Creating a va_list

      "P. Hari Krishna" <hkpammiNoSpam@ NoSpamgmail.com > wrote in message
      news:pan.2004.0 9.08.09.19.22.1 28732@NoSpamgma il.com...[color=blue]
      > 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 programmaticall y create a "va_list" and
      > then
      > pass it to the function that can the use to do some sprintfs ?[/color]

      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



      Comment

      • Moonie

        #4
        Re: Creating a va_list


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



        --
        Moonie
        ------------------------------------------------------------------------
        Posted via http://www.codecomments.com
        ------------------------------------------------------------------------

        Comment

        • Moonie

          #5
          Re: Creating a va_list


          vssprint should be vsprintf

          so sorry about that.

          Marc Smith.....Mooni e



          --
          Moonie
          ------------------------------------------------------------------------
          Posted via http://www.codecomments.com
          ------------------------------------------------------------------------

          Comment

          • P. Hari Krishna

            #6
            Re: Creating a va_list

            On Wed, 08 Sep 2004 10:29:17 -0400, Eric Sosman wrote:
            [color=blue]
            > P. Hari Krishna wrote:[color=green]
            >> 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 programmaticall y create a "va_list" and then
            >> pass it to the function that can the use to do some sprintfs ?[/color]
            >
            > 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.[/color]
            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


            Comment

            Working...