Is void *function() a pointer to function or a function returning a void*?

Viewed 9604

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 )() )();

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 typedefs:

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 is

  • function() a function that takes no arguments

  • void* function() and returns a void*.

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 is
  • arr[42] an array of 42 elements which are
  • *arr[42] pointers to
  • (*arr[42])() functions that take no arguments and
  • int *(*arr[42])() return an int*.

It takes a bit to get used to this, but once you've understood the principle, it's easy to read those declarations unambiguously.

Related