transfering variable length argument list

A

ANDERS FLODERUS

Is it possible to transfer a variable length argument
list? What I want to do is just

void myPrintf( char *pFormat, ... ) {
char buf[256];
sprintf( buf, pFormat, ... );
 
R

Rob Williscroft

ANDERS FLODERUS wrote in
Is it possible to transfer a variable length argument
list? What I want to do is just

void myPrintf( char *pFormat, ... ) {
char buf[256];
sprintf( buf, pFormat, ... );
.
.
.
return;
}

which obviously will not compile. If possible, I would
like to do it without using the va_... macros while stepping
throu the format specifier. I suppose it would be possible
althou I do not know what type the arguments are (could
be found in the format specifier?).

#include <cstdio>
#include <cstdarg>

void myPrintf( char const *pFormat, ... )
{
using namespace std;

char buf[256];

va_list va;
va_start( va, pFormat );

vsprintf( buf, pFormat, va );


/* _vsnprintf is non standard but your compiler may provide it.
If so its *safer* than the above.
*/
//_vsnprintf( buf, sizeof buf, pFormat, va );


va_end( va );

printf( "buf = [%s]\n", buf );
}

int main()
{
myPrintf("test %d", 1 );
}

HTH.

Rob.
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top