请选择 进入手机版 | 继续访问电脑版

你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

C语言问题:us_view的值是怎么得到9479的???

[复制链接]
sincomaster 提问时间:2020-8-1 16:33 /
  1. #include<stdio.h>
  2. #include<windows.h>
  3. #include<string.h>

  4. #define        SOLID        0
  5. #define DOTTED        1
  6. #define DASHED        2

  7. #define BLUE        4
  8. #define GREEN        2
  9. #define RED                1

  10. #define BLACK        0
  11. #define YELLOW        (RED | GREEN)
  12. #define MAGENTA        (RED | BLUE)
  13. #define CYAN        (GREEN | BLUE)
  14. #define WHITE        (RED | GREEN | BLUE)

  15. #define OPAQUE                        0x1
  16. #define FILL_BLUE                0x8
  17. #define FILL_GREEN                0x4
  18. #define FILL_RED                0x2
  19. #define FILL_MASK                0xe
  20. #define BORDER                        0x100
  21. #define BODRE_BLUE                0x800
  22. #define BODER_GREEN                0x400
  23. #define BODER_RED                0x200
  24. #define BODER_MASK                0xe00
  25. #define B_SOLID                        0
  26. #define B_DOTTED                0x1000
  27. #define B_DASHED                0x2000
  28. #define STYLE_MASK                0x3000

  29. const char *colors[] =
  30. {"black","red","green","yellow","blue","magenta","cyan","white"};

  31. struct box_props
  32. {
  33.         int        opaque                                                :1;
  34.         unsigned int fill_color                        :3;
  35.         unsigned int                                        :4;
  36.         int show_border                                        :1;
  37.         unsigned int border_color                :3;
  38.         unsigned int border_style                :2;
  39.         unsigned int                                        :2;
  40. };

  41. union views
  42. {
  43.         struct box_props st_view;
  44.         unsigned short us_view;
  45. };

  46. void show_settings(unsigned short);

  47. int main(void)
  48. {
  49.         union views box = {{1,YELLOW,1,GREEN,DASHED}};
  50.         char bin_str[8 * sizeof(unsigned int) + 1];

  51.         printf("\nbox settints using unsigned int view:\n");
  52.         show_settings(box.us_view);

  53.         system("pause");
  54.         return 0;
  55. }

  56. void show_settings(unsigned short us)
  57. {
  58.         printf("box is %s.\n",(us & OPAQUE) == OPAQUE ? "opaque" : "transparent");
  59.         printf("the fill color is %s.\n",colors[(us >> 1) & 07]);
  60.         printf("border %s.\n",(us & BORDER) == BORDER ? "shown" : "not shown");
  61.         printf("the border style is");

  62.         switch (us & STYLE_MASK)
  63.         {
  64.         case B_SOLID        :printf("solid.\n");
  65.                 break;
  66.         case B_DOTTED   :printf("dotted.\n");
  67.                 break;
  68.         case B_DASHED        :printf("dashed.\n");
  69.                 break;
  70.         default                        :printf("unknown type.\n");
  71.         }

  72.         printf("the border color is %s.\n",colors[(us >> 9) & 07]);
  73. }
复制代码

收藏 评论2 发布时间:2020-8-1 16:33

举报

2个回答
sincomaster 回答时间:2020-8-1 16:43:54
11_LI.jpg

编译软件:VS2010
电脑系统:win10家庭版
问题:
此代码是C Primer Plus书中第15章的(程序清单15.4 dualview.c)

请各位帮忙看看这个us_view = 9479是怎么求到的??

本人C基础还比较差,谢谢各位啦!
butterflyspring 回答时间:2020-8-19 12:30:39
看一下 us_view 地址,编译器分配给它的,与上面哪个成员地址对其。 另外不同编译器定义会有不同,这里只要讨论与STM32相关的应用。

所属标签

相似问题

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版