Creating a va_list

  • Thread starter P. Hari Krishna
  • Start date
P

P. Hari Krishna

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
 
E

Eric Sosman

P. Hari Krishna said:
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.
 
K

Kieran Simkin

P. Hari Krishna said:
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/
 
M

Moonie

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
 
P

P. Hari Krishna

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top