473,473 Members | 2,169 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Creating a va_list

Hi,
I have a situation where I do not know the number of arguments I pass to a
function at compile time.
So is there any way, that we can programmatically create a "va_list" and then
pass it to the function that can the use to do some sprintfs ?

Thanks
-Hari
Nov 14 '05 #1
5 11462
P. Hari Krishna wrote:
Hi,
I have a situation where I do not know the number of arguments I pass to a
function at compile time.
So is there any way, that we can programmatically create a "va_list" and then
pass it to the function that can the use to do some sprintfs ?


No; there is no portable way to construct an argument
list at run-time. Every function call has a fixed number
of arguments.

I'm not sure what you're trying to do, but perhaps
the vsprintf() function might be of use.

--
Er*********@sun.com

Nov 14 '05 #2
"P. Hari Krishna" <hk***********@NoSpamgmail.com> wrote in message
news:pa****************************@NoSpamgmail.co m...
Hi,
I have a situation where I do not know the number of arguments I pass to a
function at compile time.
So is there any way, that we can programmatically create a "va_list" and
then
pass it to the function that can the use to do some sprintfs ?


Unless I've completely misunderstood your question, surely it'd make sense
to just use one argument - a pointer to a NULL terminated array of (possibly
void) pointers? -- or two arguments if you're not sure at compiletime the
type of the data you're passing to your function. Or is the problem that you
need to pass an unknown number of arguments to a varargs function that has
been defined by someone other than you? -- if this is the case, I don't
believe it's possible in standard C, although it may be possible with some
machine-specific ASM.
~Kieran Simkin
Digital Crocus
http://digital-crocus.com/
Nov 14 '05 #3

void function( char *ptr, ... )
{
va_list args;
char format[81];

va_start( args, ptr );
vssprintf( format, ptr, args );
va_end( args );
}

the moral of the story is that ptr must be in a format for sprintf
for example: "%d%c%s" etc. and format must be the length of the string
that you expect.

Marc Smith.....Moonie

--
Moonie
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

Nov 14 '05 #4

vssprint should be vsprintf

so sorry about that.

Marc Smith.....Moonie

--
Moonie
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

Nov 14 '05 #5
On Wed, 08 Sep 2004 10:29:17 -0400, Eric Sosman wrote:
P. Hari Krishna wrote:
Hi,
I have a situation where I do not know the number of arguments I pass to a
function at compile time.
So is there any way, that we can programmatically create a "va_list" and then
pass it to the function that can the use to do some sprintfs ?


No; there is no portable way to construct an argument
list at run-time. Every function call has a fixed number
of arguments.

I'm not sure what you're trying to do, but perhaps
the vsprintf() function might be of use.

The arguments are in the form of a list (or an array of pointers to
strings)and so I cannot know the number of arguments at compile time.
But I think I have got the solution (portable). All I have in the
formatting string is only a bunch of %s, so I wrote a small sprintf-like
function that will go and parse the formatting string for %s and form
the resulting buffer, manually.

Thanks anyway.
-Hari
Nov 14 '05 #6

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

Similar topics

3
by: Douwe | last post by:
I try to build my own version of printf which just passes all arguments to the original printf. As long as I keep it with the single argument version everything is fine. But their is also a version...
2
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...
2
by: j0mbolar | last post by:
given this example: void bar(const char *fmt, ...) { va_list ap; va_start(ap, fmt); baz(fmt, ap); va_end(ap);
7
by: Jon | last post by:
Is it possible to modify an argument of a va_list and then pass the modified va_list to other functions? void c_fun1(int, va_list args) { int *firstIntPointer = &(va_arg(args, int)); ...
11
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...
7
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...
1
by: skillzero | last post by:
Is there a portable way to pass a va_list as a parameter to another function taking a variable argument list? I have a function that takes a printf-like format string and I'd like to use...
5
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
On Jun 3, 3:23 am, Jesse Ziser <d...@spam.diewrote: The relevant paragraph from the Standard is: ---- Begin Quote ---- The type declared is va_list which is an object type suitable for...
6
by: Laurent Deniau | last post by:
When I compile the code below with gcc -std=c99 -W -Wall -pedantic -O3 -Winline, it reports the following: variadic.c: In function ‘fv’: variadic.c:12: warning: function ‘fv’ can never be...
1
by: Chuck Chopp | last post by:
I have some code that is being built on the following: Windows Server 2003, both 32-bit & 64-bit editions Windows Vista, both 32-bit & 64-bit editions Windows Server 2008, both 32-bit & 64-bit...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.