问题描述:

I have this function takes more than one char parameters.how to print each of them and add a '\n' at the end of each char?

void printAndSave(char* msg,...)

{

//printing single one.

//printf("Log:%s\n",msg);

//saveToLog(msg);

//how to print all of them?

}

网友答案:

I think you will need to change the signature of printAndSave() to specify the number of char* arguments being passed. For example:

void printAndSave(const unsigned int arg_count, ...)
{
    unsigned int i;
    char* val;
    va_list vl;

    va_start(vl,arg_count);
    for (i=0;i<arg_count;i++)
    {
        val=va_arg(vl,char*);
        printf ("%s\n",val);
    }
    va_end(vl);
}
网友答案:

Look for example at the end of this page. You should easily adapt it for your problem ;) Please note you have to know the type of them.

相关阅读:
Top