cheater cheater - 4 months ago 22
C Question

the difference between void (*xmlHashScanner) and void *xmlHashScanner

What is the difference between

void (*xmlHashScanner)(void *payload, void *data, xmlChar *name)
and
void *xmlHashScanner(void *payload, void *data, xmlChar *name)
in C?


Why do they behave differently?



xmlHashScanner
is a user-defined function in the library
libxml2
.


When I try to redefine this function with a little different prototype:
void *xmlHashScanner
instead of
void (*xmlHashScanner)
I have the following error:

error: ‘xmlHashScanner’ redeclared as different kind of symbol
void *xmlHashScanner(void *payload, void *data, xmlChar *name)
^
In file included from /usr/include/libxml2/libxml/parser.h:18:0,
from /home/solar/Bureau/parser/src/diam_dict.c:12:
/usr/include/libxml2/libxml/hash.h:88:16: note: previous declaration of ‘xmlHashScanner’ was here
typedef void (*xmlHashScanner)(void *payload, void *data, xmlChar *name);


I wonder what is the difference between two of them.

Answer
void (*xmlHashScanner)(void *payload, void *data, xmlChar *name)

declares xmlHashScanner as a pointer to a function returning void, whereas

void *xmlHashScanner(void *payload, void *data, xmlChar *name)

declares xmlHashScanner as a function returning a pointer to void.

In both declaration and expression syntax, the unary * operator has lower precedence than the postfix [] subscript and () function call operators, so

T *a[N];     // a is an N-element array of pointer to T
T (*a)[N];   // a is a pointer to an N-element array of T
T *f();      // f is a function returning a pointer to T
T (*f)();    // f is a pointer to a function returning T