passing va_list

Started by
0 comments, last by nagromo 20 years, 1 month ago
I am trying to write a function that takes variable parameters like printf and attempts to pass them on to printf. The function right now just passes the va_list on to printf. Here is my code:

#include <iostream>
#include <stdarg.h>

enum {OOC,CHAT,INFO,DEBUG};

void print(int type,char *txt,...)
{
	va_list args;
	va_start(args,txt);
	printf(txt,args);
	va_end(args);
}

int main()
{
	int n=-50;
	print(DEBUG,"%i",n);
	return 0;
}
It does nothing now, but later sprintf will be used to decide where to put it based on the type. My problem is that it returns -1073742616. It returns the same value every time, even after code changes and recompiles. When I change it to printf and remove the DEBUG arguement, it gives -50 as expected. Am I passing the arguement list wrong?
Advertisement
Use vprintf, not printf.

vprintf( txt, args );

Similarly, use vsprintf for putting it in a string:

vsprintf( str, txt, args );

~CGameProgrammer( );

Screenshots of your games or desktop captures -- Upload up to four 1600x1200 screenshots of your projects, registration optional. View all existing ones in the archives..
~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.

This topic is closed to new replies.

Advertisement