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.

- 10,103
- 1
- 59
- 71

- 313
- 1
- 3
- 6
-
Yes, unfortunately too little thought was put into it when they chose to allow both `int* x` and `int *x` as the way to declare pointers. – goodvibration Oct 22 '19 at 09:15
-
5@goodvibration C was made format-free (and C++ "inherited" this). Even `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. – Scheff's Cat Oct 22 '19 at 09:17
-
@Scheff: The more options you give to a community of programmers, the harder it becomes to maintain, update and extend the code. Maybe it's good for the overall job-security though... – goodvibration Oct 22 '19 at 09:19
-
3@goodvibration the more you try to protect the programmer from doing what they want the more you get something like java ;) – 463035818_is_not_an_ai Oct 22 '19 at 09:20
-
2@goodvibration Less options, less flexibility. And, please, keep in mind that it's decades ago when they did it. It's easy to complain afterwards... ;-) – Scheff's Cat Oct 22 '19 at 09:20
-
@Scheff: Like I said, too little thought was put into that spcific decision. Possibly because at the time, there weren't too many programming language standards around to learn from and improve. – goodvibration Oct 22 '19 at 09:22
-
2In the C language, `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. – Stephen M. Webb Oct 22 '19 at 18:42
-
This says that the result is a `void` if you ever write `*function()`, i.e., when you dereference the result of a call to `function`. So it is a function that returns a pointer to void. – Hagen von Eitzen Oct 22 '19 at 22:09
-
1@StephenM.Webb [You cannot dereference a `void *`](https://stackoverflow.com/a/11629682/3982001). After all, even if you could, what would you do with a `void`? – Fabio says Reinstate Monica Oct 22 '19 at 22:48
-
@FabioTurati yes, only that's exactly what the function definition says. The only thing you can do with the value returned is cast it to some other type that when dereferenced will yield a value of a sensible type and of course of the original type of the value (otherwise you have UB due to pointer aliasing). The difference in the type calculus between C and C++ is subtle but important. – Stephen M. Webb Oct 23 '19 at 14:30
-
@StephenM.Webb *In the C language, `void *function()` is a function taking a* fixed (number and type) but unspecified *number of arguments* (and any mismatch is UB)...; cf `void *function(...)` which takes an arbitrary number of arguments – pmg Oct 23 '19 at 15:52
-
Does this answer your question? [What is the function of an asterisk before a function name?](/q/8911230/) – outis Aug 08 '22 at 06:43
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 )() )();

- 301,070
- 26
- 186
- 335
-
2That's why, I prefer to write `void* function();`. That's not that tempting... ;-) (The edit happened just while writing this.) – Scheff's Cat Oct 22 '19 at 09:14
-
in the code i declare `void * reader();` then on `pthread_create(&thread1,null,reader,reader_arg)` instead of `pthread_create(&thread1,null,&reader,reader_arg)` – user9515151 Oct 22 '19 at 09:19
-
1
-
@user9515151 Sorry, I wrote nonsense. `void* reader();` is a declaration of a function `reader` returning `void*`. In this case, `reader` and `&reader` provide the address of that function. In opposition, `void (*p)();` is a pointer to function (type `void()`). Now, `p` and `&p` make a difference. (Damn, you have to be careful...) – Scheff's Cat Oct 22 '19 at 09:34
-
3*Or a pointer to function that returns pointer to function* That's what `typedef` is for... ;-) – Andrew Henle Oct 22 '19 at 09:41
-
@user9515151 Made a [**Demo on coliru**](http://coliru.stacked-crooked.com/a/8f67fd1813529960) for this. (I must admit that casting a function pointer to a data pointer is not quite correct (AFAIK). Some platforms may not forgive.) – Scheff's Cat Oct 22 '19 at 09:41
-
1@AndrewHenle With typedef there us no problem. A problem arises when declarations are used without typedef or an alias declaration.:) – Vlad from Moscow Oct 22 '19 at 09:42
-
@VladfromMoscow: Given `typedef int foo(int);`, the declaration `foo bar,boz;` is equivalent to the prototypes `int bar(int); int boz(int);`. Could be useful if trying to minimize the size of header files (not sure why that wasn't done back in the day when headers were read from floppy disks), but typedef isn't a magic cure-all for confusion. – supercat Oct 22 '19 at 19:09
-
you're free to write `void *(function());` if you like - arguably, `void* function();` has other issues (what does `void* a(), b();` declare)? – Random832 Oct 22 '19 at 21:55
-
For readers of this cryptic function pointer declaration language++, the ["Clockwise rule"](http://c-faq.com/decl/spiral.anderson.html) is extremely useful. – Tejas Kale Oct 23 '19 at 08:31
-
Is the answerer saying that `void ( *function )();` is the same as `void * ( *function )();`? Don't they return different types? The first one returns nothing, whereas the second returns a void* pointer – peractio Jun 09 '20 at 06:47
-
@peractio I updated the post to make it more clear relative to your question. – Vlad from Moscow Jun 09 '20 at 12:14
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.

- 43,959
- 6
- 69
- 99
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.

- 15,171
- 8
- 38
- 76
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.

- 38,891
- 9
- 62
- 106
-
-
1@NirajGautam The same way you call any other function in C: `func()` Note that the function declaration `void* func();` must appear before the call. If there is no such function declaration, C unfortunately makes a wrong assumption about the return type of the function (assuming `int` instead of `void*`) which may lead to funny behavior of the successfully compiled program. – cmaster - reinstate monica Oct 09 '22 at 20:53