How could the unknown number of arguments been passed to printf

S

srdgame

Hi all

The example code is below:

#include <cstdio>
#include <cstdarg>

void my_printf(const char* source, const char* format, ...)
{
va_list ap;
va_start(ap, format);
vprintf(format, ap);
//char buf[1024];
//vsprintf(buf, format, ap);
//printf("%s", buf);
//va_end(ap);
}
void my_printf_2(const char* source, const char* format, ...)
{
// How to call my_printf??
my_printf(xxxxxx);
}

int main()
{
my_printf("TEST", "\tString: %s \n", "sssss");
}

And my question is how could I call my_printf in my_printf_2 ??

Thanks for your time.
 
B

Ben Bacarisse

srdgame said:
void my_printf(const char* source, const char* format, ...)
{
va_list ap;
va_start(ap, format);
vprintf(format, ap);
//char buf[1024];
//vsprintf(buf, format, ap);
//printf("%s", buf);
//va_end(ap);
}
void my_printf_2(const char* source, const char* format, ...)
{
// How to call my_printf??
my_printf(xxxxxx);
}
And my question is how could I call my_printf in my_printf_2 ??

This is a FAQ. See: http://c-faq.com/varargs/handoff.html
 
N

Nate Eldredge

srdgame said:
Hi all

The example code is below:

#include <cstdio>
#include <cstdarg>

Is this C or C++? In C, you should be including <stdio.h> and
void my_printf(const char* source, const char* format, ...)
{
va_list ap;
va_start(ap, format);
vprintf(format, ap);
//char buf[1024];
//vsprintf(buf, format, ap);
//printf("%s", buf);
//va_end(ap);
}
void my_printf_2(const char* source, const char* format, ...)
{
// How to call my_printf??
my_printf(xxxxxx);
}

int main()
{
my_printf("TEST", "\tString: %s \n", "sssss");
}

And my question is how could I call my_printf in my_printf_2 ??

You cannot do so portably. You should instead write a my_vprintf
function which takes a va_list argument, just as vprintf does, and call
it in the same way that you're calling vprintf from my_printf.
 
B

Ben Pfaff

srdgame said:
void my_printf_2(const char* source, const char* format, ...)
{
// How to call my_printf??
my_printf(xxxxxx);
}

This is in the C FAQ:

15.5: How can I write a function that takes a format string and a
variable number of arguments, like printf(), and passes them to
printf() to do most of the work?

A: Use vprintf(), vfprintf(), or vsprintf().

Here is an error() function which prints an error message,
preceded by the string "error: " and terminated with a newline:

#include <stdio.h>
#include <stdarg.h>

void error(char *fmt, ...)
{
va_list argp;
fprintf(stderr, "error: ");
va_start(argp, fmt);
vfprintf(stderr, fmt, argp);
va_end(argp);
fprintf(stderr, "\n");
}

See also question 15.7.

References: K&R2 Sec. 8.3 p. 174, Sec. B1.2 p. 245; ISO
Secs. 7.9.6.7,7.9.6.8,7.9.6.9; H&S Sec. 15.12 pp. 379-80; PCS
Sec. 11 pp. 186-7.

(By the way, your program is actually written in C++.)
 
S

srdgame

于 Thu, 05 Mar 2009 09:28:20 -0800,Nate Eldredge写到:
Is this C or C++? In C, you should be including <stdio.h> and
<stdarg.h>. If it's C++, try comp.lang.c++.
Sorry for this, I was afraid to get the suggestion that "using stream
stuff". So I did post this to comp.lang.c.
void my_printf(const char* source, const char* format, ...) {
va_list ap;
va_start(ap, format);
vprintf(format, ap);
//char buf[1024];
//vsprintf(buf, format, ap);
//printf("%s", buf);
//va_end(ap);
}
void my_printf_2(const char* source, const char* format, ...) {
// How to call my_printf??
my_printf(xxxxxx);
}

int main()
{
my_printf("TEST", "\tString: %s \n", "sssss");
}

And my question is how could I call my_printf in my_printf_2 ??

You cannot do so portably. You should instead write a my_vprintf
function which takes a va_list argument, just as vprintf does, and call
it in the same way that you're calling vprintf from my_printf.
I did search all the webs, I have got the idea that using va_list is the
only way I could choose. THanks for your help

Yours,
srdgame
 
S

srdgame

于 Thu, 05 Mar 2009 09:28:47 -0800,Ben Pfaff写到:
This is in the C FAQ:

15.5: How can I write a function that takes a format string and a
variable number of arguments, like printf(), and passes them to
printf() to do most of the work?

A: Use vprintf(), vfprintf(), or vsprintf().

Here is an error() function which prints an error message, preceded by
the string "error: " and terminated with a newline:

#include <stdio.h>
#include <stdarg.h>

void error(char *fmt, ...)
{
va_list argp;
fprintf(stderr, "error: ");
va_start(argp, fmt);
vfprintf(stderr, fmt, argp);
va_end(argp);
fprintf(stderr, "\n");
}

See also question 15.7.

References: K&R2 Sec. 8.3 p. 174, Sec. B1.2 p. 245; ISO Secs.
7.9.6.7,7.9.6.8,7.9.6.9; H&S Sec. 15.12 pp. 379-80; PCS Sec. 11 pp.
186-7.

(By the way, your program is actually written in C++.)

:) yes, indeed I am c++ guy. Appreciate your helps
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top