It is currently Tue, 07 Dec 2021 02:48:16 GMT



 
Author Message
 passing variable number arguments from function to function
Hi,

I was wondering if there is way to achieve something like the following:

void func2 (char *fmt, ....) {
    /* do the real processing */
    .....
    .....

void func1 (char *fmt, ...) {

   /* do some checks like buffer available, device ready etc etc */
   ........
   .......
    func2(fmt);   /* here i want to pass all the arguments received by
func1() as they are to func2() */

int main () {
    func1("\n%s: xyz = %d", __FUNCTION__, xyz);

Essentially, i want to pass all the arguments that func1() receives as they
are to func2() after doing some checks. Note that
both func1() and func2() should take variable number of arguments and they
must be functions (not macros, with macros this
problem is easily solvable). So, my question can we achieve this by using
functions ? If so, can someone please provide a
solution ?

Thanks,
Sarat



 Mon, 31 Jan 2005 09:00:26 GMT   
 passing variable number arguments from function to function

"Sarat Babu Kamisetty" <sar...@cisco.com> writes:

It would be difficult (ie. not portable) to do it calling directly func2.
But you could write a vfunc2 function expecting a va_list parameter.
See the case of printf(3) and vprintf(3).
Something like:

    #include <stdarg.h>

    func1(fmt,...)
    {
        va_list args;
        va_start(args,fmt);
        vfunc1(fmt,args);
        va_end(args);
    }

    vfunc1(fmt,va_list args)
    {
        vfunc2(fmt,args);
    }

    func2(fmt,...)
    {
        va_list args;
        va_start(args,fmt);
        vfunc2(fmt,args);
        va_end(args);
    }

    vfunc2(fmt,va_list args)
    {
        while(need_argument(fmt)){
            argtype  argument=va_arg(args);
        }
    }

--
__Pascal_Bourguignon__                   http://www.informatimago.com/
----------------------------------------------------------------------
 The name is Baud,...... James Baud.



 Mon, 31 Jan 2005 11:11:09 GMT   
 passing variable number arguments from function to function

I believe you can do something like this (untested, basically
pseudo-code). I have done it before, but my sample code isn't handy:

void func2(char * fmt, va_list & foo)
{
        vsnprintf(fmt, 128, fmt, foo);

void func1(char * fmt, ...)
{
        va_list ap;
        va_start(ap, fmt);

        func2(fmt, ap);

        va_end(ap);

Rgz,

Luke.



 Mon, 31 Jan 2005 11:40:15 GMT   
 passing variable number arguments from function to function
"Sarat Babu Kamisetty" <sar...@cisco.com> writes:

I do not believe this is possible.
The usual workaround goes like this:

    void func2v(char *fmt, va_list args)
    {
        /* do the real processing */
        ...
    }
    void func2 (char *fmt, ....) {
        va_list args;
        va_start(args, fmt);
        func2v(fmt, args);
        va_end(args);
    }

    void func1 (char *fmt, ...) {
        va_list args;
        /* do some checks like buffer available, device ready etc etc */
        ...
        va_start(args, fmt);
        func2v(fmt, args); /* pass all arguments to func2v */
        va_end(args);
    }

Cheers,
--
In order to understand recursion you must first understand recursion.



 Mon, 31 Jan 2005 13:04:57 GMT   
 
   [ 4 post ] 

Similar Threads

1. functions with variable number of arguments

2. Passing a variable number of parameters in a function

3. arguments passed to a function

4. Passing arguments to functions

5. passing function name as xargs argument

6. How to pass argument in functions

7. Passing arguments to ksh functions

8. Problem with variable argument functions ... (stdarg)

9. function call with variable arguments

10. Variable name as function's argument


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software