Typedef function vs function pointer

Viewed 6098

I'm trying to understand function declaration using typedefs.

What does this code do in C++?

typedef void fcn_t(void);
typedef void (*ptr_t)(void);

fcn_t f;
fcn_t *pf;
ptr_t pf2;

In my understanding:

  • fcn_t is the type of a function, and so the line with f is a function declaration (not a definition), and I could later define it like void f(void) { blabla(); bleble(); } just as if I had declared void f(void); instead of fcn_t f;;
  • fcn_t * is the type of a function pointer, and the line with pf is a pointer variable definition, and pf is default-initialized (assuming the code excerpt is from the global scope);
  • There is no difference between fcn_t* and ptr_t, thus everything I said about pf applies to pf2.

Did I get it right? Would any of the three declarations have its meaning changed if I marked them extern? What would change if the code was compiled as C instead of as C++?

2 Answers
Related