Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

redirect args

Posted on 2011-02-17
14
Medium Priority
?
448 Views
Last Modified: 2012-05-11
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
Comment
Question by:learningunix
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 3
  • +1
14 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 34918276
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
 

Author Comment

by:learningunix
ID: 34918312
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
 
LVL 31

Accepted Solution

by:
Zoppo earned 1000 total points
ID: 34918321
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 31

Expert Comment

by:Zoppo
ID: 34918344
Sorry, I needed to long to write before posting ...
0
 

Author Comment

by:learningunix
ID: 34918408
hmmm... that means in C++ one cannot pass the arg list to other function.
0
 
LVL 86

Expert Comment

by:jkr
ID: 34918530
Sorry, Zoppo is right - the 'void_2()' would have to take a 'va_list' as an argument.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 34922888
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
 
LVL 31

Expert Comment

by:Zoppo
ID: 34923906
Sorry, phoffric, but I don't see relevant differences between my posted code and yours ...
0
 
LVL 32

Expert Comment

by:phoffric
ID: 34923980
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
 
LVL 31

Expert Comment

by:Zoppo
ID: 34924021
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
 
LVL 32

Expert Comment

by:phoffric
ID: 34924115
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
 
LVL 31

Expert Comment

by:Zoppo
ID: 34924640
Ah - sorry. Yes, that's a mistake in my (not tested) code - thanks for pointing that out.
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 34924792
@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
 

Author Closing Comment

by:learningunix
ID: 34976764
thx
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question