tags:

views:

104

answers:

3

Hi I have a problem with this code. When i am using this function I have no warnings. :

    void handler(int sig){
   switch(sig) {
     case SIGINT : { click++; fprintf(stdout,"SIGINT recu\n");
                           if( click == N){
                             exit(0);
                           }
   }
     case SIGALRM : fprintf(stdout,"SIGALRM received\n");
                    exit(0);
     case SIGTERM:  fprintf(stdout,"SIGTERM received\n");
                     exit(0);


  }
  }

But when i rewrite the function with this new version, I have a " comparison between pointer and integer" warning on the if statement:

void handler( int sig){
   printf("Signal recu\n");
    if( signal == SIGINT){
     click++;
     fprintf(stdout,"SIGINT received; Click = %d\n",click);
     if(click == N){
      fprintf(stdout,"Exiting with SIGINT\n");
       exit(0);
     }
   } else if(signal == SIGALRM){
      fprintf(stdout,"SIGALRM received\n"); 
      exit(0);
   } else if(signal == SIGTERM){
     fprintf(stdout,"SIGTERM received\n"); 
     exit(0);
   }

Can someone tell me where is the prob?

+10  A: 

In the second code, you are comparing signal, which is not even a local variable in the code. In fact, signal as you've used it is probably referring to the signal function.

In contrast, in the first code, you are switching on sig, which is an int parameter to the function.

Mark Rushakoff
Yes, `signal()` is in fact a function.
Randy Proctor
Yes, `signal` is a function, and it's *probably* what `signal` is in the above code... but for all we know, he didn't `#include <signal.h>` but instead did `extern int SIGINT; /* ... */ void* signal = malloc(3);`.
Mark Rushakoff
You are totally right. In the second bunch of code, I should compare sig variable and not signal which is function. It's a careless mistalke. My bad ;)Thanks for your answer ;)
Dimitri
@Dimitri: If this solved your problem, you should accept the answer. Your accept rate is currently 50%, which is fairly low; as a result, some people will not answer your questions since they will believe you won't accept their answers.
Mark Rushakoff
+1  A: 

In second version you are comparing signal which is not defined in your function to SIGINT etc. signal is declared elsewhere to be a pointer.

Perhaps you meant to rename sig in the function prototype to signal

Mark
+4  A: 

In the second block you are comparing against signal, not sig. Since signal isn't declared locally, you're actually comparing against a pointer to the signal() function.

JSBangs