Managing variable argument call

S

sphinx.moro

Hello,
I have a function which is coded to receive an unknown number of
arguments
void Process(int i_ss, int Nerr, ...) {
va_list pa; /* variable argument list*/
va_start(pa, Nerr);
sprintf(FMT, "SS=%d, Er=%d ", i_ss, Nerr);
strcat(FMT, "A1=<%s>, A2=<%s> ");
vsprintf(line, FMT, pa);
va_end(pa);
printf("%s\n", line);
fflush(stdout);
}
Here, for simplicity, I treat2 args ,
I want to call this function from a input scenario that will give
textually the type, and value of arguments
I will decode this scenario to call the function
For example a line of scenario could be:
S=P1 S=P2
The I will cal with 2 strings
S=P1 S=P2 I=13
The I will cal with 2 strings and one int
Obviouly I need to have a format which is suitable for that.
--
I have made some experience with "alloca"
for allocation space in the stack, "pushing" arguments on the stack
and calling the function.
snippet: ARGS has been set previously
-------------------------------------------------------------------------------------
parguments = (void*) alloca(size); /* 8 in the case depicted
(2 char pointers) */
for (p=0; p < nargs;p++){
/* Get the code for the argument
*/
switch(ARGS[p].code){
case 'S' :
*(((char**)parguments)++) = ARGS[p].str; //P1
break;
case 'I' :
/* value
*/
*(((int*)parguments)++) = ARGS[p].vali; //P2
break;
default:
;
}
}
Process(i_ss, NumError);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top