void *x; is a valid statement.
void x; is not a valid operator.
A function returning a pointer to void is also true.
Why is this so?
When a variable is declared in a function, the compiler must allocate memory space for the variable. But when the variable is of type void, the compiler does not know how many bytes are allocated for this variable. So this will not work for the compiler. However, a pointer to void is different. The pointer can be of type void, which can be read as int or double or float or short or char while reading. In this case, the compiler performs explicit type conversion or automatic type advancement.
eg.
int function_A( void *x ) { int *p = (int *)x; return *p; } double function_B( void *x ) { double *p = (double *)x; return *p; }
Important Note: C does not allow the direct dereferencing type of the void pointer. I mean, you cannot do this:
double function_B( void *x ) { return (double)*x; }
Conceptually, this makes sense. But C does not allow this.
source share