Thread: Variable Argument Functions

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    75

    Question Variable Argument Functions

    How might I accomplish the following:

    Code:
    void BufferFunc(const char* format, ...)
    {
         // NOTE: this call doesnt work! but it is what I want to do!
         Func(format, ...);
         // extra processing with Func()
    }
    
    void Func(const char* format, ...)
    {
      // some junk that prolly only works 1/2 the time
    }
    Thx, sorry if im being to brief.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    Check out the tutorials at this site(cprogramming.com). There is one on this exact topic.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    75

    im afraid it doesnt

    Yes, they have a tutorial on variable argument lists but only on how to use them. They do not explain how to pass them on to a similar function.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Say you have
    Code:
    void BufferFunc(const char* format, ...)
    {
         // NOTE: this call doesnt work! but it is what I want to do!
         printf(format, ...);
         // extra processing with Func()
    }
    There is no way to do this - you only have one shot at ... parameters, and you deal with them using varargs (see various tutorials)

    Code:
    void BufferFunc(const char* format, ...)
    {
         vprintf(format, va_list);
         // extra processing with Func()
    }
    You can pass a va_list from function to function as much as you like.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    75

    thats what i thought

    Yes, i thought that might be the only way, but that would mean you just couldn't do it if there isn't va_list accepting type of function available as well. Unless someone knows how to manipulate the stack behind the scenes real well. which i definently can't.

    Thx anyway.

  6. #6
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    > Unless someone knows how to manipulate the stack behind the scenes

    You could parse the format string to obtaining the parameter types, then obtain their values as offsets from the address of the pointer to the format string (depending on the stack width). However, this would be highly non-portable.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > but that would mean you just couldn't do it if there isn't va_list accepting type of function available as well.
    This isn't a problem for the standard library, which has (i think) a 'v' equivalent function for all the functions which accept ... parameters (like printf and vprintf)

    So even if you want to call your own function, you would just need to re-arrange the code like so

    Code:
    void vFunc ( const char *format, va_list ap ) {
      // do va_arg to scan down the list of args in ap
    }
    
    void Func(const char* format, ...)
    {
      // do va_start, va_end etc, and call vFunc to do all the work
    }
    
    void BufferFunc(const char* format, ...)
    {
      // do va_start, va_end etc, and call vFunc to do all the work
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 07-26-2007, 01:08 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 07:32 AM
  4. I'll have a Variable argument list to go...
    By SMurf in forum C Programming
    Replies: 6
    Last Post: 02-27-2003, 02:02 PM
  5. rerouting variable argument lists...
    By doubleanti in forum C++ Programming
    Replies: 9
    Last Post: 12-14-2001, 09:28 AM