Hi,
i came across a function definition:
char* abc(char *f, ...)
{
}
i am finding it hard to know how this code is working...
what do the three dots mean??
Thanks.
Hi,
i came across a function definition:
char* abc(char *f, ...)
{
}
i am finding it hard to know how this code is working...
what do the three dots mean??
Thanks.
They are called an elipsis and they mean that the function can take an indeterminate number of parameters. Your function can probably be called like this:
abc( "foo", 0 );
abc( "foo", "bar", 0 );
There needs to be a way of indicating the end of the list. This can be done by using the first parameter, as ion a printf(0 format string, or by a special terminator, zero in the example above.
Functions with a variable number of parameters are considered bad form in C++, as no type checking or user defined conversions can be performed on the parameters.
The ellipses mean that there are a variable number of arguments following. The place you will have used them (perhaps without knowing) are the printf family of functions.
They allow you to create functions of that style where the parameters are not known beforehand, and you can use the varargs functions (va_start
, va_arg
and va_end
) to get at the specific arguments.
This link here has a good treatise on the printf use of varargs.
This is what is called a varargs function or a variable argument function in C.
One you'll probably recognise is printf.