In C++ these two declarations of a function
void test();
and
void test(void);
are equivalent and mean that the function does not take any arguments.
In C, the first function declaration declares a function with an empty identifier list, and the second function declaration declares a function with a parameter type list that in fact has no parameters.
According to the C Standard (6.9.1 Function definitions):
5 If the declarator includes a parameter type list, the declaration of
each parameter shall include an identifier, except for the special
case of a parameter list consisting of a single parameter of type
void, in which case there shall not be an identifier. No declaration
list shall follow.
This declaration of the function (that is also its definition)
void test(void*)
{
//...
}
has 1 parameter, an object of type void *
(pay attention to that pointers are always complete types), that is not used within the function.
This declaration is valid in C++, and is not valid in C, because in C each function parameter in a function declaration with a parameter type list that is at the same time its definition shall have an identifier, except in the case when a void
parameter is used, as specified in the quote above.
In C you may use such a declaration only when the declaration is not at the same type its definition.
So in C for example you may write like
void test(void*); // declaratipon
and then
void test( void *ptr ) // declaration and definition
{
//...
}
test()
andtest(void)
are equivalent in C++, but not in C. Unless C compatibility is required, the former form should be preferred. The functiontest(void*)
differs from the other two; it takes an unnamed argument of typevoid*
. – Mckeevervoid
is “nothing”,void*
is “the location of something”. There’s a big difference between nothing at all and a place. – Phiovoid*
is essentially a typeless pointer, a raw memory address. It can point to any "object." You can set it tonull
to have it point to nothing. – Heliogabalusvoid f(void)
is a C-ism. In C++ you don't need that and wouldn't normally do that. You'd just dovoid f()
. – Musick