passing a va_list to sprintf

S

Shea Martin

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
 
N

nrk

Shea said:
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.
 
P

Peter Pichler

Shea Martin said:
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.
 
S

Shea Martin

Shea said:
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
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top