views:

743

answers:

4

Hi, I'm trying to write a function that accepts a variable number of parameters like printf, does some stuff, then passes the variable list to printf. I'm not sure how to do this, because it seems like it would have to push them onto the stack.

Something approximately like this

http://pastie.org/694844

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

void forward_args( const char *format , ... ){
  va_list arglist;
  printf( format, arglist );
}


int main (int argc, char const *argv[]){
  forward_args( "%s %s\n" , "hello" , "world" );  return 0;
}

Any ideas?

+2  A: 

You'll be passing along the arglist value to a function designed to consume it. See the stdarg and vprintf documentation for more clues.

Gary Jackson
A: 

I'm not (off the top of my head) familiar with how to implement this. I would suggest looking at an implementation of functions like printf. Several open source implementations exist. glibc, uclibc (not sure what bsd and opensolaris call/use for their libc).

xenoterracide
+2  A: 

Are you per chance looking for va_start() / vprintf() / vsnprintf() / va_end(), or are you trying to implement those yourself?

Your question is not quite clear in that regard.

Tim Post
+11  A: 

Don't pass the results to printf. pass them to vprintf. vprintf specifically exists to handle passing in va_list arguments. From the Linux man page:

#include <stdio.h>

int printf(const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);
int sprintf(char *str, const char *format, ...);
int snprintf(char *str, size_t size, const char *format, ...);

#include <stdarg.h>

int vprintf(const char *format, va_list ap);
int vfprintf(FILE *stream, const char *format, va_list ap);
int vsprintf(char *str, const char *format, va_list ap);
int vsnprintf(char *str, size_t size, const char *format, va_list ap);

Notice how the latter explicitly take va_list arguments such as the ones you declare inside a function taking ... in the parameter list. So your function would be declared like this:

void forward_args( const char *format , ... ){
   va_list arglist;
   va_start( arglist, x );
   vprintf( arglist, format );
   va_end( arglist );
}
quark
Well, he needs to `va_start()` and `va_end()` also.
Ken Bloom
@Ken Bloom: You are right. Fixed.
quark
`va_start(arglist, format)`
Ken Bloom
@Ken Bloom: Oof. I thought I fixed that. Moral: Don't write code tired, even code you are just cutting and pasting. Thanks for catching that.
quark
Hi, Ken, I saw your name and had to think for about 15 minutes to figure out where I recognized it from... I've seen you on the Ruby mailing list ^_^ Thanks for the help, both Ken and quark.
Joshua Cheek