Search code examples
cpointerskeil

How can I correctly assign a pointer to a call back function?


I try to call a function in a IRQ with C, with the next code I get it.

static void (*functionPulsacion)();

void eint2_init(void *funcPulsacion){
    functionPulsacion = funcPulsacion;
}

But when I compile in Keil the IDE show me the next message:

Button2.c(38): warning: #513-D: a value of type "void *" cannot be assigned to an entity of type "void (*)()"

What is the good way for do this?.

Thank you in advance


Solution

  • It's the same syntax for the parameter variable as for the static one.

    static void (*functionPulsacion)(void);
    
    void eint2_init(void (*funcPulsacion)(void)) {
        functionPulsacion = funcPulsacion;
    }
    

    Typedefs make function pointers a lot more readable, though.

    typedef void (*PulsacionFunc)(void);
    
    static PulsacionFunc pulsacion_func;
    
    void eint2_init(PulsacionFunc a_pulsacion_func) {
       pulsacion_func = a_pulsacion_func;
    }