免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5242 | 回复: 15
打印 上一主题 下一主题

pointers are not permitted as case values编译错误 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-08-24 09:20 |只看该作者 |正序浏览

  1. #include<unistd.h>
  2. #include<signal.h>
  3. void show_handler(struct sigaction * act)
  4. {
  5.         switch (act->sa_flags)//这里出错,我强制类型转换也不行
  6.         {
  7.                 case SIG_DFL:
  8.                         printf("Dault action\n");
  9.                         break;
  10.                 case SIG_IGN:
  11.                         printf("Ignore the signal\n");
  12.                         break;
  13.                 default:
  14.                         printf("0x%x\n",act->sa_handler);
  15.         }
  16. }
  17. main()
  18. {
  19.         int i;
  20.         struct sigaction act,oldact;
  21.         act.sa_handler = show_handler;
  22.         act.sa_flags = SA_ONESHOT|SA_NOMASK;
  23.         sigaction(SIGUSR1,&act,&oldact);
  24.         for(i=5;i<15;i++)
  25.         {
  26.                 printf("sa_handler of signal %2d =",i);
  27.                 sigaction(i,NULL,&oldact);
  28.         }
  29. }
复制代码


编译时提示:
sigaction.c: In function `show_handler':
sigaction.c:7: pointers are not permitted as case values
sigaction.c:10: pointers are not permitted as case values
sigaction.c: In function `main':
sigaction.c:21: warning: assignment from incompatible pointer type

论坛徽章:
0
16 [报告]
发表于 2013-07-16 11:40 |只看该作者
强制转化(int)SIG_IGN.就可以了,结贴吧,哈哈。

论坛徽章:
1
IT运维版块每日发帖之星
日期:2016-08-10 06:20:00
15 [报告]
发表于 2011-04-27 11:15 |只看该作者
还没有解决呀?改为 if 语句?

  1.         if (SIG_DFL == action->sa_flags) {
  2.                 printf("Default action\n");
  3.         } else if (SIG_DFL == action->sa_flags) {
  4.                 printf("Ignore the signal\n");
  5.         } else {
  6.                 printf("0x%x\n", action->sa_handler);
  7.         }
复制代码

论坛徽章:
0
14 [报告]
发表于 2006-08-26 11:35 |只看该作者
case 后面的标号只能是 整型常量表达式。SIG_DFL 和 SIG_DFL 是函数指针,但是指针不是整数类型。

>> case语句只能是int类型或可以提升到int类型的常量表达式。

这样说不是太全面。比如 long int 常量可以作为 case 标号使用,但是上面的说法并不包括这种情况。

论坛徽章:
0
13 [报告]
发表于 2006-08-26 10:59 |只看该作者

  1. sign.c: 在函数 ‘show_handler’ 中:
  2. sign.c:11: 错误: 指针不能用作 case 常量
  3. sign.c:14: 错误: 指针不能用作 case 常量
  4. sign.c: 在函数 ‘main’ 中:
  5. sign.c:28: 警告: 从不兼容的指针类型赋值
复制代码


真正的问题在这里:


  1. 11          case SIG_DFL:
  2. 12                    printf("Dault action\n");
  3. 13                    break;
  4. 14          case SIG_IGN:
  5. 15                    printf("Ignore the signal\n");
  6. 16                    break;
复制代码

论坛徽章:
0
12 [报告]
发表于 2006-08-26 10:21 |只看该作者
请具体说明一下这个数据类型

论坛徽章:
0
11 [报告]
发表于 2006-08-25 22:17 |只看该作者
原帖由 iwinux 于 2006-8-24 10:30 发表

  1. temp = act->sa_flags;
复制代码

试一下这样

不行啊,还没解决,有人知道吗?

论坛徽章:
0
10 [报告]
发表于 2006-08-24 10:30 |只看该作者

  1. temp = act->sa_flags;
复制代码

试一下这样

论坛徽章:
0
9 [报告]
发表于 2006-08-24 09:47 |只看该作者
原帖由 iwinux 于 2006-8-24 09:42 发表
用临时变量存储指针的值

  1. #include<unistd.h>
  2. #include<signal.h>
  3. void show_handler(struct sigaction * act)
  4. {
  5.         int temp;

  6.         temp = (*(int*)&act->sa_flags);
  7.         switch(temp)
  8.         {
  9.                 case SIG_DFL:
  10.                         printf("Dault action\n");
  11.                         break;
  12.                 case SIG_IGN:
  13.                         printf("Ignore the signal\n");
  14.                         break;
  15.                 default:
  16.                         printf("0x%x\n",act->sa_handler);
  17.         }
  18. }
  19. main()
  20. {
  21.         int i;
  22.         struct sigaction act,oldact;
  23.         act.sa_handler = show_handler;
  24.         act.sa_flags = SA_ONESHOT|SA_NOMASK;
  25.         sigaction(SIGUSR1,&act,&oldact);
  26.         for(i=5;i<15;i++)
  27.         {
  28.                 printf("\nsa_handler of signal %2d =",i);
  29.                 sigaction(i,NULL,&oldact);
  30.         }
  31. }
复制代码


为什么呢?
sigaction.c: In function `show_handler':
sigaction.c:10: pointers are not permitted as case values
sigaction.c:13: pointers are not permitted as case values
sigaction.c: In function `main':
sigaction.c:24: warning: assignment from incompatible pointer type

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
8 [报告]
发表于 2006-08-24 09:46 |只看该作者
show_handler的参数应该是int类型吧?
  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP