JJS
JJS

Reputation: 1156

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

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 :)

Upvotes: 16

Views: 23715

Answers (5)

Edward Clements
Edward Clements

Reputation: 5152

void modifyAndPrintMessage(char* message,...)
{   char newMessage[1024]; // **Make sure the buffer is large enough**
    va_list args;
    va_start(args, message);
    vsnprintf(newMessage, message, args);
    printf(newMessage);
    fflush(stdout);
}

Upvotes: 4

Paul
Paul

Reputation: 22005

You can use va_list from stdarg.h,

C example: http://www.tutorialspoint.com/cprogramming/c_variable_arguments.htm C++ example: http://www.cprogramming.com/tutorial/lesson17.html.

An of course, see the man page: http://linux.die.net/man/3/stdarg

Man page example for reference:

#include <stdio.h>

#include <stdarg.h>

void
foo(char *fmt, ...)
{
    va_list ap;
    int d;
    char c, *s;

   va_start(ap, fmt);
    while (*fmt)
        switch (*fmt++) {
        case 's':              /* string */
            s = va_arg(ap, char *);
            printf("string %s\n", s);
            break;
        case 'd':              /* int */
            d = va_arg(ap, int);
            printf("int %d\n", d);
            break;
        case 'c':              /* char */
            /* need a cast here since va_arg only
               takes fully promoted types */
            c = (char) va_arg(ap, int);
            printf("char %c\n", c);
            break;
        }
    va_end(ap);
}

Upvotes: 0

K Scott Piel
K Scott Piel

Reputation: 4380

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 );
}

Upvotes: 17

Parker Kemp
Parker Kemp

Reputation: 765

There is a library which includes this functionality. Here is some example code from the reference:

#include <stdarg.h>     /* va_list, va_start, va_arg, va_end */

int FindMax (int n, ...)
{
    int i,val,largest;
    va_list vl;
    va_start(vl,n);
    largest=va_arg(vl,int);
    for (i=1;i<n;i++)
    {
        val=va_arg(vl,int);
        largest=(largest>val)?largest:val;
    }
    va_end(vl);
    return largest;
}

The ellipsis is actually valid code, and you can use the va_list object to parse a variable number of parameters.

Upvotes: -2

msam
msam

Reputation: 4287

Use varargs to accept variable number of parameters then use sprintf to create the new message

Upvotes: 0

Related Questions