I'm confused about the meaning of void *function()
.
Is it a pointer to function or a function returning void*
? I've always used it on data structures as a recursive function returning a pointer, but when i saw a code in multithreading (pthread
) there is a same function declaration. Now I'm confused what's the difference between them.
4 Answers
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 )() )();
-
2That's why, I prefer to write
void* function();
. That's not that tempting... ;-) (The edit happened just while writing this.) Oct 22, 2019 at 9:14 -
in the code i declare
void * reader();
then onpthread_create(&thread1,null,reader,reader_arg)
instead ofpthread_create(&thread1,null,&reader,reader_arg)
Oct 22, 2019 at 9:19 -
1
-
3Or a pointer to function that returns pointer to function That's what
typedef
is for... ;-) Oct 22, 2019 at 9:41 -
1@AndrewHenle With typedef there us no problem. A problem arises when declarations are used without typedef or an alias declaration.:) Oct 22, 2019 at 9:42
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.
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.
Declarations in C/C++ are read from the identifier outwards following operator precedence.
A quick look at the C/C++ operator precedence table in wikipedia reveals that the function call operator ()
has a higher precedence than the indirection operator *
. So, your function declarations reads like this:
Start at the identifier:
function
isfunction()
a function that takes no argumentsvoid* function()
and returns avoid*
.
This general principle also holds with array declarations ([]
also has higher precedence than *
) and combinations of the two. So
int *(*arr[42])();
is read as
arr
isarr[42]
an array of 42 elements which are*arr[42]
pointers to(*arr[42])()
functions that take no arguments andint *(*arr[42])()
return anint*
.
It takes a bit to get used to this, but once you've understood the principle, it's easy to read those declarations unambiguously.
void*function();
is syntactically correct. E.g. for Python they chose a different decision - format is part of syntax. IMHO, both ways have its pro and con.void *function()
is a function taking an arbitrary number of arguments and returning a value that, when dereferenced, is of type void. In C++,void* function()
is a function taking no arguments and returning a value of pointer-to-void. You should make up your mind on which language you're asking about.void *
. After all, even if you could, what would you do with avoid
?