Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typedef function pointer?

I'm learning how to dynamically load DLL's but what I don't understand is this line

typedef void (*FunctionFunc)(); 

I have a few questions. If someone is able to answer them I would be grateful.

  1. Why is typedef used?
  2. The syntax looks odd; after void should there not be a function name or something? It looks like an anonymous function.
  3. Is a function pointer created to store the memory address of a function?

So I'm confused at the moment; can you clarify things for me?

like image 402
Jack Harvin Avatar asked Nov 28 '10 04:11

Jack Harvin


People also ask

What is typedef in function pointer?

A typedef, or a function-type alias, helps to define pointers to executable code within memory. Simply put, a typedef can be used as a pointer that references a function.

What is the use of typedef function in C?

typedef is a reserved keyword in the programming languages C and C++. It is used to create an additional name (alias) for another data type, but does not create a new type, except in the obscure case of a qualified typedef of an array type where the typedef qualifiers are transferred to the array element type.

How do you pass a function pointer as an argument?

Pass-by-pointer means to pass a pointer argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the variable to which the pointer argument points. When you use pass-by-pointer, a copy of the pointer is passed to the function.

What is a function pointer in C++?

A function pointer is a variable that stores the address of a function that can later be called through that function pointer. This is useful because functions encapsulate behavior.


2 Answers

  1. typedef is used to alias types; in this case you're aliasing FunctionFunc to void(*)().

  2. Indeed the syntax does look odd, have a look at this:

    typedef   void      (*FunctionFunc)  ( ); //         ^                ^         ^ //     return type      type name  arguments 
  3. No, this simply tells the compiler that the FunctionFunc type will be a function pointer, it doesn't define one, like this:

    FunctionFunc x; void doSomething() { printf("Hello there\n"); } x = &doSomething;  x(); //prints "Hello there" 
like image 25
Jacob Relkin Avatar answered Sep 18 '22 23:09

Jacob Relkin


typedef is a language construct that associates a name to a type.
You use it the same way you would use the original type, for instance

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

using them like

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

As you can see, you could just replace the typedefed name with its definition given above.

The difficulty lies in the pointer to functions syntax and readability in C and C++, and the typedef can improve the readability of such declarations. However, the syntax is appropriate, since functions - unlike other simpler types - may have a return value and parameters, thus the sometimes lengthy and complex declaration of a pointer to function.

The readability may start to be really tricky with pointers to functions arrays, and some other even more indirect flavors.

To answer your three questions

  • Why is typedef used? To ease the reading of the code - especially for pointers to functions, or structure names.

  • The syntax looks odd (in the pointer to function declaration) That syntax is not obvious to read, at least when beginning. Using a typedef declaration instead eases the reading

  • Is a function pointer created to store the memory address of a function? Yes, a function pointer stores the address of a function. This has nothing to do with the typedef construct which only ease the writing/reading of a program ; the compiler just expands the typedef definition before compiling the 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() to calculate 123*456 
like image 55
Déjà vu Avatar answered Sep 17 '22 23:09

Déjà vu