Home > Back-end >  What does second argument here "typedef void fv(int), (*pfv)(int)"
What does second argument here "typedef void fv(int), (*pfv)(int)"

Time:02-21

Here is code which disturbing me:

typedef void fv(int), (*pfv)(int)

It seems it does definition of function fv which takes int as first argument, but what does mean here second part (*pfv)(int)?

CodePudding user response:

It has two typedefs; one defines a typedef called fv that points to a function type and the other a pointer-to-function type pfv.

typedef void fv(int), (*pfv)(int)

Indeed, one could have instead written

typedef void fv(int);
typedef fv *pfv;

i.e. define fv as a typedef for the function type, and define pfv as a pointer to the said function type.

Note that in nowhere are we defining or declaring a function; however you can use the fv typedef then to declare a function:

fv foo;

is the same as declaring

void foo(int);

And finally, it is more opinionated, but generally using typedefs to hide pointers is not preferred, so instead of using pfv to define a pointer to the function type, you could just use fv * to declare these pointers everywhere:

fv *funcp = foo;

CodePudding user response:

A pointer to a function taking an integer argument and returning void. A hint comes from the variable name. pointer to a function returning void.

Expanding

typedef void fv(int);
typedef void (*pfv)(int);

The brackets here are necessary (*pfv) to distinguish from the case

void *fpv(int);

a function returning a pointer to a void.

You may find this article helpful.

  • Related