久芯网

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

avatar sincomaster 提问时间: 2023-09-24 20:10: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个回答
  • avatar sincomaster
    回答时间: 2023-09-24 20:34:21

    编译软件:VS2010 电脑系统:win10家庭版 问题: 此代码是C Primer Plus书中第15章的(程序清单15.4 dualview.c) 请各位帮忙看看这个us_view = 9479是怎么求到的?? 本人C基础还比较差,谢谢各位啦!

  • avatar butterflyspring
    回答时间: 2023-09-24 20:53:12

    看一下 us_view 地址,编译器分配给它的,与上面哪个成员地址对其。 另外不同编译器定义会有不同,这里只要讨论与STM32相关的应用。

会员中心 微信客服
客服
回到顶部