PDA

View Full Version : functions with ANY number of arguments



glinsvad
January 25th, 2006, 06:10 PM
Hi

I'm building my own class for string manupulations in C/C++ to refresh my programming skills a little (let's just say I used Win98 the last time I compiled anything :).

Basically I want to create my own sprintf, so I need to figure out how one declares a function that can accept ANY number of arguments of ANY type char/char*/int/float etc. Naturally function overloading is out of the question:
char* sprintf(char*,char);
char* sprintf(char*,char*);
char* sprintf(char*,int);
char* sprintf(char*,float);
char* sprintf(char*,char,char);
char* sprintf(char*,char,char*);
...

If I don't want to cast the information into a list of strings (char** like in main-funtion) before calling my sprintf, how can it be done? Or can't it?

Cheers

LordHunter317
January 25th, 2006, 07:06 PM
You use '...' in the argument declaration and the va_args macros in <stdargs.h>

glinsvad
January 25th, 2006, 07:19 PM
Thanks man

Further info:
I googled stdarg.h (not stdargs.h apparently...) and found a short description
http://www.opengroup.org/onlinepubs/007908799/xsh/stdarg.h.html