none
chains in C/C++ [pointers to functions]

    Question

  • Hi: I've been programming (and testing) code in C for 2 years, and I ran across these function declarations while I was reviewing certain code.

    At first I thought I was dealing with some sort of CAST, or a pointer to function...later I found some indications about "chaining"...I don't understand the use of the parenthesis before (void) -first example- or (pointer) -second example.

    Could somebody explain the use of those parenthesis (for instance "the inner parenthesis should return this or that type of variable, and then a  CAST -again, I'm saying this as an example- is executed with the outer parenthesis,  or a pointer to function is created considering...etc )....

    NOTE: My biggest confusion is with the (pointer) and (void) parenthesis to the right...I cannot see exactly their use......here is the code!:


    function definitions:

    void (* _int_get_isr(int vector_num))(void)
    {
    /* function body: not shown  */
    }
     
     
    or a similar one, a little bit more complicated:
     
     
    void (* _int_get_isr2(int vector_num))(pointer)
    {
    /* function body: not shown */
    }

     
    Here is a more complete code (for a better understanding):
     
    typedef struct {
    pointer OLD_ISR_DATA;  
    void (* OLD_ISR)(pointer);    /* here there is NO doubt that OLD_ISR is a pointer to a function, taking a pointer as an argument, returning void */ 
    int TICK_COUNT;
    } MY_ISR_STRUCT, * MY_ISR_STRUCT_PTR;
     
     
    void main_task ( int initial_data )
    {
     MY_ISR_STRUCT_PTR isr_ptr;
    isr_ptr->TICK_COUNT = 0;
    isr_ptr->OLD_ISR = int_get_isr2(INTEGER_NUMBER);     
    /*etc*/
    }


    AND FINALLY, A LINE THAT MENTIONS "CHAIN" taken from a different section 

    /* Chain to previous notifier */
    (*isr_ptr->OLD_ISR)(isr_ptr->OLD_ISR_DATA); /* NOT A MULTIPLICATION, OF COURSE...BUT I DONIT UNDERSTAND

                                                                        WHY 2 PARENTHESIS...


    I WOULD APPRECIATE YOUR COMMENTS. THANK YOU IN ADVANCE
    Friday, March 06, 2009 5:01 PM

Answers

  • It is a function call.  The first set of parentheses ensure * binds to the function pointer, not the function result, the 2nd set are the parentheses you need to call a function.  It is completely unnecessary syntax, the author of this code appeared to have obfuscated this intentionally.  I propose this syntax instead:

    typedef void* pointer;
    typedef void (* FunctionTakingVoid)(void);
    typedef void (* FunctionTakingPointer)(pointer);

    FunctionTakingVoid _int_get_isr(int vector_num)
    {
    }


    FunctionTakingPointer _int_get_isr2(int vector_num)
    {
    }


    typedef struct {
      pointer OLD_ISR_DATA; 
      FunctionTakingPointer OLD_ISR;
      int TICK_COUNT;
    } MY_ISR_STRUCT, * MY_ISR_STRUCT_PTR;


    void main_task ( int initial_data )
    {
      MY_ISR_STRUCT_PTR isr_ptr;
      isr_ptr->TICK_COUNT = 0;
      isr_ptr->OLD_ISR = _int_get_isr2(INTEGER_NUMBER);    
      /* Chain to previous notifier */
      isr_ptr->OLD_ISR(isr_ptr->OLD_ISR_DATA);
    }


    Hans Passant.
    • Marked as answer by Nancy Shao Tuesday, March 10, 2009 2:28 AM
    Saturday, March 07, 2009 12:32 PM