473,807 Members | 2,857 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing a va_list to sprintf

I have a function:

void f(char* format, ...)
{
va_list args;
char buf[128];
sprintf(buf, format, args);
printf(buf);
}

But I can't get sprintf to properly take the args I have given to f(). I can't
seem to find any examples on passing a va_list.

Any help would be appreciated.

Thanks.

~S

Nov 13 '05 #1
3 28239
nrk
Shea Martin wrote:
I have a function:

void f(char* format, ...)
{
va_list args;
char buf[128];
sprintf(buf, format, args);
printf(buf);
}

But I can't get sprintf to properly take the args I have given to f(). I
can't seem to find any examples on passing a va_list.

Any help would be appreciated.

Thanks.

~S


What you want is vsprintf and not sprintf. Remember to call va_start before
making the call to vsprintf and call va_end after the call returns. Look
up your favorite C book or manual for more details.

-nrk.
Nov 13 '05 #2
"Shea Martin" <sh**@snowsquir rel.ca> wrote in message
news:kooob.2309 08$pl3.90584@pd 7tw3no...
I have a function:

void f(char* format, ...)
{
va_list args;
char buf[128];
sprintf(buf, format, args);
printf(buf);
}

But I can't get sprintf to properly take the args I have given to f(). I can't seem to find any examples on passing a va_list.

Any help would be appreciated.


Try vsprintf.
Nov 13 '05 #3
Shea Martin wrote:
I have a function:

void f(char* format, ...)
{
va_list args;
char buf[128];
sprintf(buf, format, args);
printf(buf);
}

But I can't get sprintf to properly take the args I have given to f().
I can't seem to find any examples on passing a va_list.

Any help would be appreciated.

Thanks.

~S

Thanks for the solutions, worked perfect.
~S

Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2070
by: Simon L | last post by:
Morning all, I'm converting my windows exe into a windows service and to get some log information I'm replacing TRACE with my own Log. Since some of the Trace statements take a number of arguments I want to be able to stuff any number of arguments into my log, as in sprintf. eg Log( "%s %i Hello", "Hi", 23) gives me 122334 !£., Hello with the following code.
2
2581
by: OneLG13 | last post by:
I have a program that uses a scripting language that does not support formating strings with a printf syntax. Thus, I am writing a plugin to add the functionality. The entry point and argument passing of the scripting language to my plugin does not use standard c va_arg/list arguments for arbitrary number of arguments but a c++ container object to achieve a similar approach. I thought I could just construct my own array with the arguments...
2
3348
by: Joerg Schoen | last post by:
Hi folks! I have a function that gets a 'va_list'. I am passing the 'va_list' two times to a function like 'vprintf' to print it out. I thought that this was portable until I came across a platform (AS/400) where it fails: The 'va_list' is actually defined as some some kind of an array, so passing it happens by *reference*: The first user will destroy it and the second
2
4725
by: Andrej Prsa | last post by:
Hi! If I declare two functions like this: int execute_command (char *name, ...) { va_list args; va_start (args, name); my_func (1, args);
9
1936
by: Matt Garman | last post by:
I'm trying to develop (for my own personal use) a general "messaging facility" in C. The idea is that the facility will be used for both debugging/logging and user interfacing. I'd like it to be fairly portable, but as robust and reliable as possible. My intent is to develop an API for such general messaging. I'll implement the parts that write to stdout, stderr and files. Later, I'll add support for writing to (for example) syslog,...
11
3077
by: thierrydollar | last post by:
Hi, I have written a very simple program using variable arguments calls and I get strange things that I cannot explain. I have one function (add) that displays two parameters. It works well when I call it directly. Now I have a second function (set) that also calls add. But this doesn't
5
7793
by: coltrane | last post by:
Is it possible to have a function with variable number of parameters pass the parameters to another method that takes variable number of params? maybe something like this void vfoo2( const char * format, ....) { va_list parg; va_start(parg, format);
7
2865
by: Flash Gordon | last post by:
Reading the standard, va_list is an object type (, so I believe the following should be possible: #include <stdarg.h> void foo(va_list *arg) { /* do some stuff which conditionally might read parameters off arg */ }
1
4009
by: Anup Gupta | last post by:
imsc_int32_t imsc_wsprintf( OUT imsc_wchar_t *s, IN imsc_wchar_t *format, ...) { va_list ap; imsc_int32_t dNumBytes; va_start(ap, format); dNumBytes = swprintf(s,format, ap); va_end(ap);
0
10626
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10372
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10112
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
9193
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...
0
6879
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();...
0
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4330
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
3854
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
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.