• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 449
  • Last Modified:

redirect args

How to redirect args to different function

The reason for doing this is because the implemention has now moved from vout to vout_2. So I just need to redirect it to vout_2 (which is an external define)

The definition of vout_2 is

vout_2 (int, ## __VA_ARGS__)
#include <stdio.h>                                                              
#include <stdarg.h>                                                             
                                                                                
void vout(int max, ...);                                                        
void vout_2(int max, ...);                                                        
                                                                                
int main(void)                                                                  
{                                                                               
   vout(3, "Sat", "Sun", "Mon");                                                                           
}                                                                               

void vout(int max, ...)                                                         
{                                                                               
   // I want to call vout_2 by redirecting the args. What should be the parameter lis
   vout_2(max, <Not sure what should be here> )
}
                                                                                
void vout_2(int max, ...)                                                         
{                                                                               
   va_list arg_ptr;                                                             
   int args = 0;                                                                
   char *days[7];                                                               
                                                                                
   va_start(arg_ptr, max);                                                      
   while(args < max)                                                            
   {                                                                            
      days[args] = va_arg(arg_ptr, char *);                                     
      printf("Day:  %s  \n", days[args++]);                                     
      }                                                                         
   va_end(arg_ptr);                                                             
}

Open in new window

0
learningunix
Asked:
learningunix
  • 6
  • 3
  • 3
  • +1
1 Solution
 
jkrCommented:
Make that like
void vout(int max,...)                                                         
{                                                                               
   va_list args;
   va_start(args,max);
 
   vout_2(max, args);

   va_end(args);
}

Open in new window

0
 
learningunixAuthor Commented:
Here's what I tried but the output is garbage


Insie void_2
Day:
Day:  ØZ
Day:  Mon

#include <stdio.h>
#include <stdarg.h>

void vout(int max, ...);
void vout_2(int max, ...);

int main(void)
{
   vout(3, "Sat", "Sun", "Mon");
   printf("\n");
}

void vout(int max, ...)
{
   va_list arg_ptr;
   va_start(arg_ptr, max);
   vout_2(max, arg_ptr);
   va_end(arg_ptr);
}


void vout_2(int max, ...)
{
   va_list arg_ptr;
   int args = 0;
   char *days[7];

   printf("Insie void_2 \n");

   va_start(arg_ptr, max);
   while(args < max)
   {
      days[args] = va_arg(arg_ptr, char *);
      printf("Day:  %s  \n", days[args++]);
      }
   va_end(arg_ptr);
}

Open in new window

0
 
ZoppoCommented:
Hi learningunix,

IMO this is not possible - the only thing you can do is to retrieve the va_list in 'vout' and pass it to 'vout_2', somehow like this:


void vout_2( int max, va_list& args ); 

void vout(int max, ...)                                                         
{                                                                               
 va_list args;
 va_start(args, max);

 vout_2( args );

 va_end(args);
}

void vout_2( int max, va_list& args )
{
  int args = 0;                                                                
  char *days[7];                                                               
                                                                                
  while(args < max)                                                            
  {                                                                            
   days[args] = va_arg( args, char *);                                     
   printf("Day:  %s  \n", days[args++]);                                     
  }                                                                         
}

Open in new window



I'm not sure if that somehow helps you, but I don't see any other possibility ...

ZOPPO
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ZoppoCommented:
Sorry, I needed to long to write before posting ...
0
 
learningunixAuthor Commented:
hmmm... that means in C++ one cannot pass the arg list to other function.
0
 
jkrCommented:
Sorry, Zoppo is right - the 'void_2()' would have to take a 'va_list' as an argument.
0
 
phoffricCommented:
Below is a program with output:
Sat
Sun
Monday

At each break at the printf statement (around Line 23), I took a memory dump of days[ i ] (which, as you can see, are collocated).Memory Dump: days[0], days[1], days[2]
I used VS 2010 Express, which, for C++ is an excellent debugger. If you use another compiler, it actually helps to compile using two systems as the error message on one may be easier to follow than on another.

If you haven't already done so, you can download the free Visual Studio Expresss C++ 2010:
    http://www.microsoft.com/express/Downloads/

I like the VS C++ debugger so much that I wrote articles on it. To quickly get started (about 15 minutes learning curve), you can read:

   C/C++ Beginner's Debugging Guide

After becoming familiar with the basics, move onto these two articles:
   Breakpoint Tips for C/C++

   Watch, Memory, Stack Tips: C/C++


#include <stdio.h>
#include <stdarg.h>
#include <string.h>

void vout_2( int max, va_list &args );

void vout(int max ...)
{
   va_list args;
   va_start(args, max);

   vout_2( max, args );

   va_end(args);
}  

void vout_2( int max, va_list& args2 )
{
   char *days[7];
   for(int i = 0; i< max; ++i)
   {
      days[i] = va_arg( args2, char *);
      printf("%s\n", days[i]);
   }
}

int main() {
   vout( 3, "Sat", "Sun", "Monday");
}

Open in new window

0
 
ZoppoCommented:
Sorry, phoffric, but I don't see relevant differences between my posted code and yours ...
0
 
phoffricCommented:
Sorry, Zoppo, but when I saw the comment
    "hmmm... that means in C++ one cannot pass the arg list to other function."
and
   "IMO this is not possible"
I thought the conclusion was just that. So, I guess I really misinterpreted what was being said, because passing the arg list down to another function is certainly doable.

I didn't evaluate your code because of these comments thinking there was a consensus that it was not doable.
0
 
ZoppoCommented:
No problem - it's a bit confuzing naming two different things ('...', 'va_list') with the same name 'arg list' ... just a communication issue :o)
0
 
phoffricCommented:
I guess I skimmed it too fast, and just missed the important gist of what you were saying. I realize now that you were just giving a schematic rather than compilable source.

So, I just compared our two posts a little more, and I see that I call vout_2 with two parameters whereas you call it with one; and what had confused me in my initial quick perusal was that in vout_2, I saw that line int args = 0 and the expression va_arg( args, char *) and thought that va_args should not be taking an int. (I see now that this latter issue is just a typo.)

Anyway, the main confusion was that I thought the consensus was that it was not doable. That's why I wrote a program with output just to make sure that it was doable.
0
 
ZoppoCommented:
Ah - sorry. Yes, that's a mistake in my (not tested) code - thanks for pointing that out.
0
 
ZoppoCommented:
@learningunix: Just a question: Are both functions written by yourself or is there any reason that you have to use variable argument list?

I ask because IMO using a variable argument list like shown here is absoluteley senseless - passing the data as an array of 'char*' in the shown sample would be more flexible (because the number of elements isn't hardcoded in a function call) and more performant since the VA-handling isn't needed.

IMO variable argument lists only make sense when the type of passed parameters isn't fixed, i.e. as with 'printf', there the parameters can have any type. In a case like yours it's sure the expected parameters have to be of type 'char*', the only variant info is the number of parameters.

So, you could write your 'v_out' in one of these two ways:

void v_out_a( int max, ... );
void v_out_b( int max, char* args[] );

IMO the only advantage of 'v_out_a' is you can directly call it as shown in your original post, i.e.:

v_out_a( 3, "Sat", "Sun", "Mon" );

while with the second form you have to call it somehow like this:

char* args[] = { "Sat", "Sun", "Mon" };
v_out_b( 3, args );

This even has an atdvantage, since you can pass the size of the passed array in a more dynamically way, i.e.

v_out_b( sizeof( args ) / sizeof( args[ 0 ] ), args );

Thus changing the code to i.e. pass a fourth day isn't that error prone as the first version since with the first version you have to change the call to

v_out_a( 4, "Sat", "Sun", "Mon", "Tue" ); // Two changes needed

and for the second call you just have to modify the array

char* args[] = {  "Sat", "Sun", "Mon", "Tue"  };


ZOPPO
0
 
learningunixAuthor Commented:
thx
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 6
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now