c++ - Typedef function pointer? -


i'm learning how dynamically load dll's don't understand line

typedef void (*functionfunc)(); 

i have few questions. if able answer them grateful.

  1. why typedef used?
  2. the syntax looks odd; after void should there not function name or something? looks anonymous function.
  3. is function pointer created store memory address of function?

so i'm confused @ moment; can clarify things me?

typedef language construct associates name type.
use same way use original type, instance

  typedef int myinteger;   typedef char *mystring;   typedef void (*myfunc)(); 

using them like

  myinteger i;   // equivalent    int i;   mystring s;    // same      char *s;   myfunc f;      // compile equally  void (*f)(); 

as can see, replace typedefed name definition given above.

the difficulty lies in pointer functions syntax , readability in c , c++, , typedef can improve readability of such declarations. however, syntax appropriate, since functions - unlike other simpler types - may have return value , parameters, lengthy , complex declaration of pointer function.

the readability may start tricky pointers functions arrays, , other more indirect flavors.

to answer 3 questions

  • why typedef used? ease reading of code - pointers functions, or structure names.

  • the syntax looks odd (in pointer function declaration) syntax not obvious read, @ least when beginning. using typedef declaration instead eases reading

  • is function pointer created store memory address of function? yes, function pointer stores address of function. has nothing typedef construct ease writing/reading of program ; compiler expands typedef definition before compiling actual code.

example:

typedef int (*t_somefunc)(int,int);  int product(int u, int v) {   return u*v; }  t_somefunc afunc = &product; ... int x2 = (*afunc)(123, 456); // call product() calculate 123*456 

Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -