473,804 Members | 4,311 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2414
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.anno unce.newusers
<http://www.geocities.c om/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html >
<http://www.netmeister. org/news/learn2quote.htm l>
<http://cfaj.freeshell. org/google/>
Dec 21 '06 #5
CBFalconer <cb********@yah oo.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@TheRi ng>,
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,2 55,fmt,va_list) ;
va_end(ap);
}
Dec 21 '06 #7
ma**********@po box.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_Keit h) 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.edu writes:
CBFalconer <cb********@yah oo.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_Keit h) 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********@yah oo.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
2802
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 relationalOp) { List list = new ArrayList();
0
1462
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 use the custom interface. What I found was that variable argument lists are not detected at all. Instead they are just converted from SAFEARRAYs to a Python list.
8
3972
by: Alex Vinokur | last post by:
Various forms of argument passing ================================= C/C++ Performance Tests ======================= Using C/C++ Program Perfometer http://sourceforge.net/projects/cpp-perfometer http://alexvn.freeservers.com/s1/perfometer.html
5
34382
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 the actual function call. I will post the code below of the structure, the prototype and and function call and if someone can explain this I would be very appreciative: struct data { float amount; string fname;
3
14960
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) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
58
10188
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 code... TCHAR myArray; DoStuff(myArray);
39
7684
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 indicate: a) That I don't know enough b) Passing arguments by ref is bad
11
4476
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 accomplish. // - - - - - - - - begin code - - - - - - - typedef int sm_t; typedef int bg_t; sm_t sm; bg_t bg;
17
3607
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 (*fcmp)() = &cmp_fcn; qsort(..., fcmp); then everything works. But if instead I code qsort as:
12
2599
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 by value temporaries are created?(I'm not touching yet the cases where standard allows optimizations from the side of implementations to avoid copying) If so, please quote part of the standard that says that. Assuming it is true, I can imagine two...
0
9589
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10329
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9163
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7626
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4304
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3830
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3000
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.