By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,119 Members | 866 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,119 IT Pros & Developers. It's quick & easy.

Passing a variable argument list

P: n/a
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!
--
-Rob Hoelz
Dec 21 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
you could use a macro, if that solves your purpose

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

-Mujoo

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.

Thanks!
--
-Rob Hoelz
Dec 21 '06 #2

P: n/a

Ashwani wrote:
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.

Dec 21 '06 #3

P: n/a

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.
The FAQ is available online. The part you need is question 15.12 -
http://c-faq.com/varargs/handoff.html

Dec 21 '06 #4

P: n/a
Ashwani wrote:
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__)
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:
<news:news.announce.newusers
<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/>
Dec 21 '06 #5

P: n/a
CBFalconer <cb********@yahoo.comwrote:
Ashwani wrote:
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__)

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!

--
-Rob Hoelz
Dec 21 '06 #6

P: n/a
2006-12-21 <20061221004807.537ebc65@TheRing>,
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, ...);
}
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);
}
Dec 21 '06 #7

P: n/a
ma**********@pobox.com writes:
Ashwani wrote:
>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.
Both C99 and gcc support variadic macros, but with differing syntax
and semantics. (I don't remember the details.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 22 '06 #8

P: n/a
Rob Hoelz <ho***@wisc.eduwrites:
CBFalconer <cb********@yahoo.comwrote:
>Ashwani wrote:
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

8-)}

2) Is there a C89 workaround I can use for non-C99 compilers?
Take a look at vfprintf() and vsprintf().

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 22 '06 #9

P: n/a
Rob Hoelz wrote:
CBFalconer <cb********@yahoo.comwrote:
>Ashwani wrote:
>>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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Dec 22 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.