Search code examples
c++cpointersfunction-pointersdeclaration

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


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.


Solution

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