function(something, ...)
just a quick question, i see people create functions in their program with the ..., what does this do?
e.g.:
void Output(char *cText, ...);
It means that cText is followed by zero or more other arguments of any type. "Famousest" example is printf.
Quote:Original post by graveyard filla
but, how, in the function, do you receive / declare these parameters for use then?
I think it's some crappy macro.
By the way, don't use this with C++ (basically anything more than PODs).
here's a little example... [smile]
#include <iostream>#include <cstdarg>// warning: this function is not thread safe :)const char* format( const char* fmt, ... ){ static char buffer[ 4096 ]; va_list arg_list; va_start( arg_list, fmt ); vsprintf( buffer, fmt, arg_list ); va_end( arg_list ); return buffer;}int main(){ const char* output; int a = 42; int b = 23; int c = 65535; float d = 29.1173f; output = format( "1st example : %d %d\n", a, b ); std::cout << output; output = format( "2nd example : 0x%x %.4f\n", c, d ); std::cout << output; return 0;}
That example is just a dissaster waiting to happen, since you are not doing bounds checking.
I think boost library has an alternative to the variable arguments for formatting strings.
I think boost library has an alternative to the variable arguments for formatting strings.
Quote:Original post by OrthoDiablo
That example is just a dissaster waiting to happen, since you are not doing bounds checking.
I think boost library has an alternative to the variable arguments for formatting strings.
well, indeed it is, but that's why it is an example only. i just thought it might help looking at some code actually.
i apologize if anyone had any problems with that snippet! [smile]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement