Passing a variable argument list

R

Rob Hoelz

Is there a way to pass the variable argument list from one function to
another? For example:

void my_func(const char *fmt, ...) {
char str[256];
snprintf(str, 255, fmt, ...);
}

I know that probably won't work, but you get my meaning.

Thanks!
 
A

Ashwani

you could use a macro, if that solves your purpose

#define my_func(fmt,...) snprintf(str, 255, fmt, ##__VA_ARGS__)

-Mujoo
 
M

mark_bluemel

Ashwani said:
you could use a macro, if that solves your purpose

#define my_func(fmt,...) snprintf(str, 255, fmt, ##__VA_ARGS__)

1) Please don't top post.
2) I don't believe that feature is standard - IIRC it's a gcc extension.
 
M

mark_bluemel

Rob said:
Is there a way to pass the variable argument list from one function to
another? For example:

void my_func(const char *fmt, ...) {
char str[256];
snprintf(str, 255, fmt, ...);
}

I know that probably won't work, but you get my meaning.

The FAQ is available online. The part you need is question 15.12 -
http://c-faq.com/varargs/handoff.html
 
C

CBFalconer

Ashwani said:
Rob said:
Is there a way to pass the variable argument list from one
function to another? For example:

void my_func(const char *fmt, ...) {
char str[256];
snprintf(str, 255, fmt, ...);
}

I know that probably won't work, but you get my meaning.

you could use a macro, if that solves your purpose

#define my_func(fmt,...) snprintf(str, 255, fmt, ##__VA_ARGS__)

Please don't top-post. I fixed this one. Read the following links.

Variadic macros require a C99 compliant system. Most aren't.

--
Some informative links:
< <http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>
 
R

Rob Hoelz

CBFalconer said:
Ashwani said:
Rob said:
Is there a way to pass the variable argument list from one
function to another? For example:

void my_func(const char *fmt, ...) {
char str[256];
snprintf(str, 255, fmt, ...);
}

I know that probably won't work, but you get my meaning.

you could use a macro, if that solves your purpose

#define my_func(fmt,...) snprintf(str, 255, fmt, ##__VA_ARGS__)

Please don't top-post. I fixed this one. Read the following links.

Variadic macros require a C99 compliant system. Most aren't.

Crap. Two questions following this up:

1) I assume there's a statement (like __cplusplus) that I can check to
see if the compiler's C99 compliant. Does anyone happen to know what
it is?

2) Is there a C89 workaround I can use for non-C99 compilers?

Thanks again!
 
R

Random832

2006-12-21 said:
Is there a way to pass the variable argument list from one function to
another? For example:

void my_func(const char *fmt, ...) {
char str[256];
snprintf(str, 255, fmt, ...);
}

void my_func(const char *fmt, ...) {
char str[256];
va_list ap;
va_start(ap,fmt);
vsnprintf(str,255,fmt,va_list);
va_end(ap);
}
 
K

Keith Thompson

1) Please don't top post.
2) I don't believe that feature is standard - IIRC it's a gcc extension.

Both C99 and gcc support variadic macros, but with differing syntax
and semantics. (I don't remember the details.)
 
K

Keith Thompson

Rob Hoelz said:
CBFalconer said:
Ashwani said:
Rob Hoelz wrote: [...]
Is there a way to pass the variable argument list from one
function to another? For example:

void my_func(const char *fmt, ...) {
char str[256];
snprintf(str, 255, fmt, ...);
}

I know that probably won't work, but you get my meaning.
[snip]
1) I assume there's a statement (like __cplusplus) that I can check to
see if the compiler's C99 compliant. Does anyone happen to know what
it is?

You can test for __STDC_VERSION__ >= 199901L.

But beware: "gcc -std=c99", for example, defines __STDC_VERSION__ as
199901L, but it doesn't fully implement the C99 standard.)

Most current compilers implement a much simpler test, though:

#if 0

:cool:}

2) Is there a C89 workaround I can use for non-C99 compilers?

Take a look at vfprintf() and vsprintf().
 
C

CBFalconer

Rob said:
CBFalconer said:
Ashwani said:
Rob Hoelz wrote:

Is there a way to pass the variable argument list from one
function to another? For example:

void my_func(const char *fmt, ...) {
char str[256];
snprintf(str, 255, fmt, ...);
}

I know that probably won't work, but you get my meaning.

you could use a macro, if that solves your purpose

#define my_func(fmt,...) snprintf(str, 255, fmt, ##__VA_ARGS__)
.... snip ...

Variadic macros require a C99 compliant system. Most aren't.

Crap. Two questions following this up:

1) I assume there's a statement (like __cplusplus) that I can check
to see if the compiler's C99 compliant. Does anyone happen to know
what it is?

#if defined(__STDC__) && (__STDC_VERSION__ >= 199901L)
/* This section for C99 up */
#else
/* This section for others */
#endif
2) Is there a C89 workaround I can use for non-C99 compilers?

Use gcc. Read its docs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top