Home > Blockchain >  How can I receive a void parameter in function definition, C or C ?
How can I receive a void parameter in function definition, C or C ?

Time:05-21

How can I receive in a function definition when the one of the parameter in function call is void?

The scenario is that one of my projects, in a function call one of the parameters is void. That section I could not change. I can change only the function declaration or the function definition, but I still confused what I need to change.

Please help

Below code is not a executable one:

for ex:

#include <stdio.h>

void test(void, int a) {
    printf("%d", a);
}

int main() {
    test(void, 32);
}

CodePudding user response:

You can't. That is not valid C:

<source>:2:11: error: 'void' must be the only parameter
    2 | void test(void,int a)
      |           ^~~~

Same goes for C :

<source>:2:11: error: invalid use of type 'void' in parameter declaration
    2 | void test(void,int a)
      |           ^~~~

The only valid use of void as parameter is on it's own, as in:

int bar();
int foo(void);

In C this means the function bar takes an undetermined number of arguments while foo does not take any arguments. In C both are equivalent.

CodePudding user response:

void is an incomplete type and cannot be used as the parameter of a function(in both C and C ) with the exception that if void is the only parameter of that function. This can be seen from the following quoted statements.

C

From function's documentation:

Parameter list determines the arguments that can be specified when the function is called. It is a comma-separated list of parameter declarations, each of which has the following syntax:

  • 5 Indicates that the function takes no parameters, it is the exact synonym for an empty parameter list: int f(void); and int f(); declare the same function. Note that the type void (possibly cv-qualified) cannot be used in a parameter list otherwise: int f(void, int); and int f(const void); are errors (although derived types, such as void* can be used). In a template, only non-dependent void type can be used (a function taking a single parameter of type T does not become a no-parameter function if instantiated with T = void).

Note also that there is a semantic difference(between C & C) in case void is used as a parameter of a function and is the only parameter as shown in the following example:

int func(void);  //declaration of func that takes no parameter and has the return type of void
int foo();     //declaration of foo that takes no parameter and has the return type of void

C

From function declaration's documentation:

parameters cannot have type void (but can have type pointer to void). The special parameter list that consists entirely of the keyword void is used to declare functions that take no parameters.

int f(void); // OK
int g(void x); // Error

Unlike in C and function definitions (since C23), the declarators f() and f(void) have different meaning: the declarator f(void) is a new-style (prototype) declarator that declares a function that takes no parameters. The declarator f() is a declarator that declares a function that takes unspecified number of parameters (unless used in a function definition)

  •  Tags:  
  • c c
  • Related