Yet another var-arg problem... 
Author Message
 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:



Fri, 03 Apr 1998 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Any error in this code (var arg problem)

2. unsigned short as var arg

3. Calling funcs from a var arg function

4. Numerical recipes, var arg function passing - how to?

5. arg c, arg[v] in borland turbo c 4.5

6. CONFLICT - Global Var of a .LIB Vs Global Var of a program which uses the LIB

7. Vararg func as an argument to another vararg func

8. var = !var;

9. extern var vs. static extern var

10. type *var -- vs. -- type* var

11. Args: var number & var types

12. Concatenate CString var + int var

 

 
Powered by phpBB® Forum Software