C: Passing variable number of arguments from one function to another

c, function, parameter-passing, variables

Solution

You want to use varargs...

void modifyAndPrintMessage( char* message, ... )
{
    // do somehthing custom

    va_list args;
    va_start( args, message );

    vprintf( newMessage, args );

    va_end( args );
}

Problem

So, here's a small problem I'm facing right now -> I'm trying to write a function that will accept a char* message and a variable number of arguments. My function will modify the message a little, and then It'll call printf with the message and given parameters. Essentialy, I'm trying to write something like that: ``` void modifyAndPrintMessage(char* message,...){ char* newMessage; //copy message. //Here I'm modifying the newMessage to be printed,and then I'd like to print it. //passed args won't be changed in any way. printf(newMessage,...); //Of course, this won't work. Any ideas? fflush(stdout); } ``` So, anybody knows what should I do to make it happen? I'd be most grateful for any help :)

Original source

Related problems