Is there C syntax for function pointer from function declaration

Instead of declaring a function pointer typedef for a function, is it possible to get it from a function declaration?

Usually,

int foo(int x); typedef int (*fooFunc)(int); fooFunc aFunc; 

What I want:

 int foo(int x); foo* aFunc; 

I want to use it for dlsym:

 foo* aFunc; aFunc = dlsym(lib, "foo"); aFunc(x); 

If I update foo and forgot to update fooFunc, or vice versa, that would be bad. In addition, I can have many functions, and it would be better to work with both function declarations and typedefs-pointers to functions that are associated with these functions.

Conclusion: AndreyT's answer is the most portable, but if you code gcc, then typeof is a great solution.

+4
source share
5 answers

If you have gcc, typeof works.

Update

 $ cat fxf.c #include <stdio.h> int main(int argc, char **argv) { typedef __typeof__ (main) function_of_same_type_as_main_t; function_of_same_type_as_main_t *f; printf("main() called.\n"); f = main; if (argc) f(0, NULL); return 0; } 
  $ / usr / bin / gcc -std = c89 -pedantic -Wall -Wextra -o fxf fxf.c
 fxf.c: 3: warning: unused parameter 'argv'
  $ ./fxf
 main () called.
 main () called.
+5
source

If you are talking specifically about the declaration, i.e. about a non-defining function declaration, you can remove redundancy by specifying typedef-name for the function type and using it in both cases - declare the function itself and declare a pointer to it, for example

 typedef int FuncType(int); /* <- function type */ FuncType foo; /* <- declaration of `int foo(int)` */ FuncType *aFunc; /* <- definition of `int (*aFunc)(int)` */ 

those. typedef names can be used in declarations that do not define a function. However, you cannot use the typedef name in the function definition, which means that later you still have to do

 int foo(int x) /* <- no way to use the above `FuncType` here */ { /* whatever */ } 

which basically makes the above trick almost useless.

Of course, this will not help you create a pointer from an existing ad without modification, if this is your situation.

+6
source

The simple answer is no, it does not work. foo is a specific function that has a prototype ( int (int) ). Using foo in the way you did this would be a bit like using int to declare another int :

 4 x; // expect this to be the same as int x 

However, there may be compiler extensions that do this work. I know that on the upcoming C ++ standard there will be a decltype keyword to allow this. Using this, the following may work (untested, since I don't have an auxiliary compiler):

 int foo(int x); decltype(&foo) aFunc = dlsym(lib, "foo"); 
+2
source

It's impossible. However, you can write code that will generate a warning so that you catch the type mismatch. The following code generates an assignment from an incompatible pointer type warning.

 #include <stdio.h> int foo(int, int); typedef int(*fooFunc)(int); fooFunc myfunc; int foo(int x, int y) { return 2*x + y; } int main(int argc, char **argv) { myfunc = foo; printf("myfunc : 0x%x\n", (unsigned int)myfunc); return 0; } 

Of course, this means that you will need to write this test code where the foo function is visible, so for each type of function this is even more code. The solution here is probably a code generator that will generate the correct header file containing both functions and their associated typedefs

0
source

Not exactly the same, but you can typedef a function and use it for both the prototype and the pointer.

 typedef int fooFunc(int); fooFunc foo; fooFunc *aFunc; 
0
source

All Articles