Re: Simple va_list question.

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Tue, 04 Nov 2008 18:43:51 -0600, "Doug Harrison [MVP]" <dsh@xxxxxxxx>
wrote:

On Tue, 4 Nov 2008 16:27:42 -0800 (PST), cheuyo <eungchang@xxxxxxxxx>
wrote:

All,

Seriously, I don't know what is wrong with me, but the following code
is not working.

void myfunc(const char* fmt, ...)
{
char buf[128];

va_list args;
va_start(args, fmt);
sprintf_s(buf,128, fmt, args);
va_end(args);

printf(buf);
}

int _tmain(int argc, _TCHAR* argv[])
{
int a = 3, b =5, c = 7;

myfunc("a=%d, b=%d, c=%d\n", a, b, c);

return 0;
}

Of course I mean to pring "a=3, b=5, c=7"
But when I run the program, it prints "a=1310316, b=1310568,
c=29966045",
clearly just random values in stack.

Can anyone tell me what is wrong with my code?

Look at vsprintf and friends.

The "friend" of vsprintf you want above is vprintf, of course. :) Then you
don't need any variant of sprintf at all.

--
Doug Harrison
Visual C++ MVP
.



Relevant Pages