开发者

Prob comparing pointers and integer in C

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){
      fprin开发者_高级运维tf(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?


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜