sincomaster 发表于 2020-8-1 16:33:04

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

#include<stdio.h>
#include<windows.h>
#include<string.h>

#define        SOLID        0
#define DOTTED        1
#define DASHED        2

#define BLUE        4
#define GREEN        2
#define RED                1

#define BLACK        0
#define YELLOW        (RED | GREEN)
#define MAGENTA        (RED | BLUE)
#define CYAN        (GREEN | BLUE)
#define WHITE        (RED | GREEN | BLUE)

#define OPAQUE                        0x1
#define FILL_BLUE                0x8
#define FILL_GREEN                0x4
#define FILL_RED                0x2
#define FILL_MASK                0xe
#define BORDER                        0x100
#define BODRE_BLUE                0x800
#define BODER_GREEN                0x400
#define BODER_RED                0x200
#define BODER_MASK                0xe00
#define B_SOLID                        0
#define B_DOTTED                0x1000
#define B_DASHED                0x2000
#define STYLE_MASK                0x3000

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

struct box_props
{
        int        opaque                                                :1;
        unsigned int fill_color                        :3;
        unsigned int                                        :4;
        int show_border                                        :1;
        unsigned int border_color                :3;
        unsigned int border_style                :2;
        unsigned int                                        :2;
};

union views
{
        struct box_props st_view;
        unsigned short us_view;
};

void show_settings(unsigned short);

int main(void)
{
        union views box = {{1,YELLOW,1,GREEN,DASHED}};
        char bin_str;

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

        system("pause");
        return 0;
}

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

        switch (us & STYLE_MASK)
        {
        case B_SOLID        :printf("solid.\n");
                break;
        case B_DOTTED   :printf("dotted.\n");
                break;
        case B_DASHED        :printf("dashed.\n");
                break;
        default                        :printf("unknown type.\n");
        }

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

sincomaster 发表于 2020-8-1 16:43:54



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

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

本人C基础还比较差,谢谢各位啦!

butterflyspring 发表于 2020-8-19 12:30:39

看一下 us_view 地址,编译器分配给它的,与上面哪个成员地址对其。 另外不同编译器定义会有不同,这里只要讨论与STM32相关的应用。
页: [1]
查看完整版本: C语言问题:us_view的值是怎么得到9479的???