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

Time:11-19

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

CodePudding user response:

Make sure the type of funcPulsacion matches that of functionPulsacion, like so:

static void (*functionPulsacion)(void);

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

It helps to use typedef to define the function pointer type so it can be reused:

typedef void (*functionPulsacion_type)(void);

static void functionPulsacion_type functionPulsacion;

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

CodePudding user response:

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;
}
  • Related