【问题标题】:Matching int with element of int array in switch case在 switch case 中将 int 与 int 数组的元素匹配
【发布时间】:2015-02-13 15:46:14
【问题描述】:

我收到以下错误:

program.c:24:3: error: case label does not reduce to an integer constant
   case proc_id[0]: //child process 1 pid

对于以下代码中的所有 case 语句:

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

int proc_id[4];

int main(int argc, char *argv[]){
    int i;

    int proc=1;
    for (i = 0; i < 3; ++i)
    {
        if(proc != 0){
            proc = fork(); 
        }
        if (proc != 0)
        {
            proc_id[i]=proc;
        }
    }

    switch(getpid()){
        case proc_id[0]: //child process 1 pid
            for (i = 0; i < l; ++i)
            {
                alarm(3);
                kill(proc_id[1], SIGUSR1);
            }
            break;

        case proc_id[1]: //child process 2 pid
            for (i = 0; i < l; ++i)
            {
                alarm(6);
                kill(proc_id[2], SIGUSR2);
            }
            break;

        case proc_id[2]: //child process 3 pid
            for (i = 0; i < l; ++i)
            {
                alarm(9);
                kill(proc_id[0], SIGUSR1);
            }
            break;

        case proc_id[3]: //parent pid
            printf("parent\n");
            break;
    }
    return(0);
}

void custom_handler(int signum){
    kill(SIGINT, getppid());
    printf("PID:%d \t Signal Number: %d\n", getpid(), signum);
}

void parent_handler(int signum){
    ++sigcount;
    switch(sigcount){
        case l:
            kill(proc_id[0], SIGTERM);
        break;
        case l+3:
            kill(proc_id[1], SIGTERM);
        break;
        case l+6:
            kill(proc_id[2], SIGTERM);
            kill(proc_id[3], SIGTERM);
        break;
    }
}

void sigterm_handler(int signum){
    exit(0);
}

我有两个问题:

  1. 为什么会出现这个错误 和
  2. 我们能否将计算结果为整数的表达式作为 case 标签?

环境细节: 在 ubuntu 终端上使用 gcc 编译程序。

【问题讨论】:

    标签: c gcc switch-statement


    【解决方案1】:

    switch 标签是在编译时计算的,所以它必须是常量表达式。你不能放置一个计算结果为任何东西的表达式。

    如果你想要一个等效的算法,你必须使用if 语句

    【讨论】:

      【解决方案2】:

      case proc_id[0]:
      

      proc_id[0] 不是常量,而是变量。案例标签只能使用const 如:

      case 10:
      

      对于其他 case 语句也是如此。

      当你有一个变量时,你唯一的选择是使用if-else 块。

      int pid = getpid();
      if ( pid == proc_id[0] )
      {
         for (i = 0; i < l; ++i)
         {
            alarm(3);
            kill(proc_id[1], SIGUSR1);
         }
      }
      else if ( pid == proc_id[1] )
      {
         for (i = 0; i < l; ++i)
         {
            alarm(6);
            kill(proc_id[2], SIGUSR2);
         }
      
      }
      else if ( pid == proc_id[2] )
      {
         for (i = 0; i < l; ++i)
         {
            alarm(9);
            kill(proc_id[0], SIGUSR1);
         }
      }
      else if ( pid == proc_id[3] )
      {
         printf("parent\n");
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-04-28
        • 2012-09-14
        • 1970-01-01
        • 1970-01-01
        • 2011-12-21
        • 2011-11-17
        相关资源
        最近更新 更多