Is there a difference in these two declarations?
int foo( int a, ... );
and
int foo( int a ... );
If there is no difference, what was the point of making the second syntactically valid?
Variadic functions are functions that can take a variable number of arguments. In C programming, a variadic function adds flexibility to the program. It takes one fixed argument and then any number of arguments can be passed.
va_list is a complete object type suitable for holding the information needed by the macros va_start, va_copy, va_arg, and va_end. If a va_list instance is created, passed to another function, and used via va_arg in that function, then any subsequent use in the calling function should be preceded by a call to va_end.
Argument. Parameter. When a function is called, the values that are passed during the call are called as arguments. The values which are defined at the time of the function prototype or definition of the function are called as parameters.
Ellipsis in C++ allows the function to accept an indeterminate number of arguments. It is also known as the variable argument list. Ellipsis tells the compiler to not check the type and number of parameters the function should accept which allows the user to pass the variable argument list.
This is speculation, but in C++ in can make sense to have a function with no other parameters, e.g. void f(...)
whereas in C a function like this has no use (that I know of) so ...
must follow some other parameter and hence, a comma.
From a grammar point of view, it's simpler to simply allow void f( int a ... )
and give it the obvious meaning than it is to disallow it and it's not going to cause much of a burden on compiler writers or any confusion for programmers.
(I originally thought it might be something to do with making the grammar for parameter packs more regular but I discovered that it was explicitly allowed in C++03 in any case.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With