2015-02-18 24 views
5

Recentemente stavo leggendo un codice, e ha scoperto che un puntatore a funzione è scritta come:Scrivi puntatore a funzione in C

int (*fn_pointer (this_args))(this_args) 

Io di solito incontrare un puntatore a funzione come questa:

return_type (*fn_pointer) (arguments); 

Simile cosa è discusso 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; 
} 

qualcuno può dirmi qual è la differenza e come doe s questo lavoro?

+0

puoi inserire il codice esatto? un campione compilabile? –

+1

Questo potrebbe aiutare http: // StackOverflow.it/questions/840501/how-do-function-pointers-in-c-work? rq = 1 –

+0

@ iharob: il mio codice è più lungo. Ho appena aggiornato. –

risposta

10
int (*fn_pointer (this_args))(this_args); 

dichiara fn_pointer come una funzione che prende this_args e restituisce un puntatore a una funzione che prende this_args come argomento e restituisce un tipo int. È equivalente a

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

Cerchiamo di capire un po 'di più:

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 readint (*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 

Così, finalmente un lavoro a casa :). Cercare di capire la dichiarazione

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

e utilizzare typedef ridefinirlo.

+0

Quale è l'argomento e quale è il ritorno? –

+0

@SHIVENDRAMISHRA; È chiaro ora? – haccks

4

Da cdecl (che è un utile strumento di supporto per decifrare le dichiarazioni C):

int (*fn_pointer (this_args1))(this_args2) 

dichiarare fn_pointer in funzione (this_args1) tornando puntatore a funzione (this_args2) tornando int

Quindi il primo è una funzione che restituisce il puntatore alla funzione, mentre il secondo:

return_type (*fn_pointer) (arguments);

è un normale "puntatore alla funzione".


Leggi di più riguardo undestanding dichiarazioni complesse dall'articolo Clockwise/Spiral Rule.

+2

'fn_pointer' non è un puntatore in questa dichiarazione (solo una funzione). – Wintermute

+0

@Wintermute: buona cattura, grazie. –

2

Questo

int (*fn_pointer (this_args1))(this_args2) 

dichiara una funzione che prende come argomenti this_args1 e restituisce un puntatore a funzione di tipo

int (*fn_pointer)(this_args2) 

quindi è solo una funzione che restituisce un puntatore a funzione.

Problemi correlati