
Yet another var-arg problem...
Quote:
>Is it possible to pass in argumnts by reference in a variable
>argument list? I'd like to make assigments to several of the
>variable passed in so that upon returning to the calling function,
>the arguments will contain NEW values.
<code snipped>
There is no such thing as pass by reference in c. Only pass by
value, even it that value happens to be the address of an object
that the called function is to modify.
In general, if you want a called function to modify an existing
copy of an object, you pass the address of that object to the
function, which of course, must expect to receive a pointer to
the that type of object.
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#define ASSIGN 0
char *bar( void)
{
static char s[] = "The static string from bar().";
return s;
}
void foo(int OP, ...)
{
va_list argp; /* points to each unnamed argument in turn*/
char **ptr;
va_start(argp, OP);
ptr = va_arg( argp, char** );
switch(OP)
{
case ASSIGN:
/* here I want to assign the retrun value of function bar
to the passed in arg 'str', but I haven't a clue
how to do it. Is it possible?*/
*ptr = bar();
break;
default:
*ptr = (char*)NULL;
}
va_end(argp);
return;
}
int main(void)
{
char *str;
foo(ASSIGN, &str);
printf("Back from foo, str is: %s\n", str);
return 0;
}
--
John R Buchan -:|:- Looking for that elusive FAQ? ftp to: