Is void *function() a pointer to function or a function returning a void*?
The function has the return type void *
.
void *function();
So I always prefer in such cases to separate the symbol *
from the function name like
void * function();
And as Jarod42
pointed to in a comment you can rewrite the function declaration in C++ using the trailing return type like
auto function() -> void *;
If you want to declare a pointer to function then you should write
void ( *function )();
where the return type is void
Or
void * ( *function )();
where the return type void *
.
Or a pointer to function that returns pointer to function
void * ( *( *function )() )();
It is a function returning a pointer to void
.
Think of your declaration this way:
void *(function());
This would be a function returning void
(or nothing):
void (*function2)();
Think of the above declaration this way:
void ((*function2)());
A much easier way to write these is to use typedef
s:
typedef void *function_returning_void_pointer();
typedef void function_returning_nothing();
function_returning_void_pointer function;
function_returning_nothing *function2;
This generally eliminates the confusion around function pointers and is much easier to read.
Whenever I'm unsure about C syntax issues, I like to use the cdecl utility (online version) to interpret for me. It translates between C syntax and English.
For example, I input your example of void *foo()
and it returned
declare foo as function returning pointer to void
To see what the other syntax would look like, I input declare foo as pointer to function returning void
and it returned
void (*foo)()
This gets particularly useful when you have multiple levels of typecasts, stars, or brackets in a single expression.