LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 2507|回复: 13

错误:case label does not reduce to an integer constant

[复制链接]
发表于 2003-6-26 20:38:27 | 显示全部楼层 |阅读模式
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #define NUM 5
  4. main()
  5. {
  6. int i,j;
  7. int pid[NUM],cpid;
  8. pid[0]=getpid();//get the pid of the parent prodecure
  9. for(i=1;i<NUM+1;i++)
  10.         {
  11.         if((cpid=fork())==-1)
  12.                 {
  13.                 perror("fork error!");
  14.                 exit(1);
  15.                 }
  16.         else
  17.         if(cpid==0)
  18.         {
  19.         pid[i]=getpid();
  20.         break;
  21.         }
  22.         else
  23.         continue;
  24.         }
  25. /*
  26. cpid=getpid();
  27. switch(cpid)
  28.         {
  29.         case pid[0]:{printf("It is the parent prode");break;}
  30.         case pid[1]:{printf("It is the first prode");break;}
  31.         case pid[2]:{printf("It is the second prode");break;}
  32.         case pid[3]:{printf("It is the third prode");break;}
  33.         case pid[4]:{printf("It is the forth prode");break;}
  34.         case pid[5]:{printf("It is the fifth prode");break;}
  35.         } */
  36. }
复制代码
我不知道我的系统里不认pid_t。只得自己用int来表示了。运行后却有以下的提示:
test.c: In function `main':
test.c:28: case label does not reduce to an integer constant
test.c:29: case label does not reduce to an integer constant
test.c:30: case label does not reduce to an integer constant
test.c:31: case label does not reduce to an integer constant
test.c:32: case label does not reduce to an integer constant
test.c:33: case label does not reduce to an integer constant
发表于 2003-6-26 23:39:04 | 显示全部楼层
case 应该是常数
因为这是编译顺必须确定的
 楼主| 发表于 2003-6-27 08:33:57 | 显示全部楼层
我修改了一下程序。能够将 pid[] 的五个元素打印出来了。还不是常数吗?
发表于 2003-6-27 09:51:53 | 显示全部楼层
常数就编译时确定
而不是运行时确定的

pid[] 什么进修都可以改变
所以会报错
 楼主| 发表于 2003-6-27 09:59:33 | 显示全部楼层
那我还有什么办法可以把五个并行进程分开呢?
发表于 2003-6-27 10:08:22 | 显示全部楼层

  1. /*
  2. cpid=getpid();
  3. switch(cpid)
  4.         {
  5.         case pid[0]:{printf("It is the parent prode");break;}
  6.         case pid[1]:{printf("It is the first prode");break;}
  7.         case pid[2]:{printf("It is the second prode");break;}
  8.         case pid[3]:{printf("It is the third prode");break;}
  9.         case pid[4]:{printf("It is the forth prode");break;}
  10.         case pid[5]:{printf("It is the fifth prode");break;}
  11.         } */
复制代码


使用if cpid ==pid[0]
...
这样做啊

语法上不支持那么就要换个方向思考
 楼主| 发表于 2003-6-27 10:11:55 | 显示全部楼层
修改完了。就是用指针代替数组。看来在写程序的时候编译这一步还是要考虑的。colored兄也在没有注意到这里不能用数组。
发表于 2003-6-27 10:23:11 | 显示全部楼层

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>

  4. #define NUM 5

  5. int main()
  6. {
  7.   pid_t pid[NUM], cpid;
  8.   int proc_number;

  9.   for(proc_number = 0; proc_number < NUM; ++proc_number){
  10.     if((pid[proc_number] = fork()) < 0)
  11.       perror("fork error");
  12.     else if(pid[proc_number] == 0)
  13.       break;
  14.   }
  15.   cpid = getpid();
  16.   if(proc_number < NUM)
  17.     printf("child %d: %d\n", proc_number + 1, cpid);
  18.   else
  19.     printf("parent: %d\n", cpid);

  20.   exit(0);
  21. }
复制代码
 楼主| 发表于 2003-6-27 21:40:15 | 显示全部楼层
libinary兄,你这个做法只是把进程的pid打印出来。要是想分开各个进程,确定每个进程的作用。就不行了。
发表于 2003-6-28 02:57:38 | 显示全部楼层
我不但打印了pid,还打印了proc_number,用这个数字就可以分开呀
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表