Writing a function pointer in c

I recently read the code and found that the function pointer is written as:

int (*fn_pointer ( this_args ))( this_args ) 

I usually come across a function pointer as follows:

 return_type (*fn_pointer ) (arguments); 

A similar thing is discussed here :

 // this is a function called functionFactory which receives parameter n // and returns a pointer to another function which receives two ints // and it returns another int int (*functionFactory(int n))(int, int) { printf("Got parameter %d", n); int (*functionPtr)(int,int) = &addInt; return functionPtr; } 

Can someone tell me what the difference is and how it works?

+5
source share
3 answers
 int (*fn_pointer ( this_args ))( this_args ); 

declares fn_pointer as a function that takes this_args and returns a pointer to a function that takes this_args as an argument and returns an int . It is equivalent

 typedef int (*func_ptr)(this_args); func_ptr fn_pointer(this_args); 

Know this a little more:

 int f1(arg1, arg2); // f1 is a function that takes two arguments of type // arg1 and arg2 and returns an int. int *f2(arg1, arg2); // f2 is a function that takes two arguments of type // arg1 and arg2 and returns a pointer to int. int (*fp)(arg1, arg2); // fp is a pointer to a function that takes two arguments of type // arg1 and arg2 and returns a pointer to int. int f3(arg3, int (*fp)(arg1, arg2)); // f3 is a function that takes two arguments of // type arg3 and a pointer to a function that // takes two arguments of type arg1 and arg2 and // returns an int. int (*f4(arg3))(arg1, arg2); // f4 is a function that takes an arguments of type // arg3 and returns a pointer to a function that takes two // arguments of type arg1 and arg2 and returns an int 

How to read int (*f4(arg3))(arg1, arg2);

  f4 -- f4 f3( ) -- is a function f3(arg3) -- taking an arg3 argument *f3(arg3) -- returning a pointer (*f3(arg3))( ) -- to a function (*f3(arg3))(arg1, arg2) -- taking arg1 and arg2 parameter int (*f3(arg3))(arg1, arg2) -- and returning an int 

So, finally, homework :). Try to find out the declaration

 void (*signal(int sig, void (*func)(int)))(int); 

and use typedef to override it.

+10
source

From cdecl (which is a handy helper for decrypting C declarations):

 int (*fn_pointer ( this_args1 ))( this_args2 ) 

declare the fn_pointer function as a function (this_args1), returning a pointer to a function (this_args2) returning an int

Therefore, the first function that returns a pointer to a function, and the second:

return_type (*fn_pointer ) (arguments);

- The usual "function pointer".


Learn more about invalid complex ads from an article in a clockwise / spiral fashion .

+4
source

it

 int (*fn_pointer ( this_args1 ))( this_args2 ) 

declares a function that takes this_args1 as an argument and returns a pointer to a function of type

 int (*fn_pointer)(this_args2) 

so this is just a function that returns a pointer to a function.

+2
source

Source: https://habr.com/ru/post/1213612/


All Articles