473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Passing a variable argument list

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
9 2393
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

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

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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Eric A. Forgy | last post by:
Hello, I am just learning Java and am trying to write a method that does something like //=========================================== public Static List find(double array,double val,String...
0
by: Philip Rittenhouse | last post by:
I have discovered a couple of problems with the way the universal gateway code handles optional parameters and variable argument lists in COM servers. It appears to only be a problem when you...
8
by: Alex Vinokur | last post by:
Various forms of argument passing ================================= C/C++ Performance Tests ======================= Using C/C++ Program Perfometer...
5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
17
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int...
12
by: dave_dp | last post by:
Hi, I have just started learning C++ language.. I've read much even tried to understand the way standard says but still can't get the grasp of that concept. When parameters are passed/returned...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.