5

I was recently reading a code, and found that a function pointer is written as :

int (*fn_pointer ( this_args ))( this_args )

I usually encounter a function pointer like this :

return_type (*fn_pointer ) (arguments);

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 somebody tell me what is the difference and how does this work ?

3
  • can you please post the exact code? a compilable sample? Commented Feb 18, 2015 at 12:38
  • 1
    This might help stackoverflow.com/questions/840501/… Commented Feb 18, 2015 at 12:42
  • @ iharob: My code is longer. I just updated. Commented Feb 18, 2015 at 12:51

3 Answers 3

9
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 argument and returns an int type. It is equivalent to

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

Let's understand it a bit 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 a home work :). Try to figure out the declaration

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

and use typedef to redefine it.

Sign up to request clarification or add additional context in comments.

1 Comment

Which one is argument and which one is return ?
3

From cdecl (which is a handy helper tool to decipher C declarations):

int (*fn_pointer ( this_args1 ))( this_args2 )

declare fn_pointer as function (this_args1) returning pointer to function (this_args2) returning int

Hence the former is a function, that returns pointer to function, while the latter:

return_type (*fn_pointer ) (arguments);

is an ordinary "pointer to function".


Read more about undestanding complex declarations from the Clockwise/Spiral Rule article.

1 Comment

fn_pointer is not a pointer in this declaration (just a function).
2

This

int (*fn_pointer ( this_args1 ))( this_args2 )

declares a function that takes as arguments this_args1 and returns a function pointer of type

int (*fn_pointer)(this_args2)

so it's just a function that returns a function pointer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.