function(something, ...)

Started by
7 comments, last by codemonster 19 years, 7 months ago
just a quick question, i see people create functions in their program with the ..., what does this do? e.g.: void Output(char *cText, ...);
---------------------------------------------think outside the quadLogic GamesI realized I stay on the pc too long when my wireless mouse died after replacing the batteries...twice
Advertisement
It means that cText is followed by zero or more other arguments of any type. "Famousest" example is printf.
Kippesoep
but, how, in the function, do you receive / declare these parameters for use then?
FTA, my 2D futuristic action MMORPG
You should search for va_list, va_start and va_end if I remember correctly.
stdarg.h is all you need
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).
Not giving is not stealing.
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.
Give a man a fish and you feed him for a day; teach him to use the Net and he won't bother you for weeks.
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