How can I solve the warning "assignment from incompatible pointer type"

2

I'm doing this program in C about signals, but when compiling it I get this warning:

  

practica8.c: In function 'main':

     

practica8.c: 18: 17: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]

     

act.sa_handler = treat_alarm; / Function to execute /

Do you know how I can fix it?

    #include <stdio.h>
    #include <signal.h>
    #include <stdlib.h>

    void *tratar_alarma(void)
    {
        printf("Alarma activada\n");
    }

    int main(void)
    {
        struct sigaction act;
        sigset_t mask;
        int pause(void);
        unsigned int alarm(unsigned int seconds);

        /*especifica el manejador*/
        act.sa_handler = tratar_alarma; /*Funcion a ejecutar*/
        act.sa_flags = 0;/*ninguna accion escifica*/

        /*Sebloquea la señal 3 SIGQUIT*/

        sigemptyset(&mask);
        sigaddset(&mask, SIGQUIT);
        sigprocmask(SIG_SETMASK, &mask, NULL);
        sigaction(SIGALRM, &act, NULL);

        for(;;)
        {
            alarm(3);/*se arma el temporizador*/
            pause();/*se suspende el proceso hasta que se reciba la
        señal*/
        }
    }
    
asked by Daniel R 27.04.2018 в 04:25
source

1 answer

3

The answer is short, you do not pass the kind of pointer you expect sigaction::sa_handler which is a pointer to function with an argument of type int that returns void .

According to wikipedia , the definition of sa_handler is as follows:

void (*sa_handler)(int);

And it's happening to you:

void *(*similar_a_tratar_alarma)(void);

The solution: Change the definition of your function tratar_alarma to the following:

void tratar_alarma(int tipo) {
  // Haz algo con la alarma.
}

And it should work:)

    
answered by 27.04.2018 в 05:59