| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 762 人关注过本帖
标题:关于C语言联合(union)的问题
只看楼主 加入收藏
b7f28954
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2015-11-30
结帖率:100%
收藏
 问题点数:0 回复次数:0 
关于C语言联合(union)的问题
#include<stdio.h>
#define YES 1
#define NO 0
//边框线的样式
#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)
struct box_props{
    unsigned int opaque       :1;
    unsigned int fill_color   :3;
    unsigned int              :4;
    unsigned int show_border  :1;
    unsigned int border_color :2;
    unsigned int border_style :2;
    unsigned int              :2;
};
union Views
{
    struct box_props st_view;
    unsigned int ui_view;
};
int main(void)
{
    union Views box={{YES,YELLOW,YES,GREEN,DASHED}};
    printf("%d\n",box.ui_view);
        return 0;
}
这个题是书上看到的、关于联合和结构的区别,我只知道结构各个成员是分家的、而联合的成员是一个家的,联合允许第一个成员被初始化。
union Views box={{YES,YELLOW,YES,GREEN,DASHED}};我在书上看到这里时;
unsigned int ui_view;这个联合成员怎么也看不懂;想问下这个值是怎么来的?
unsigned int ui_view;这个联合成员怎么也看不懂;想问下这个值是怎么来的?
unsigned int ui_view;这个联合成员怎么也看不懂;想问下这个值是怎么来的?
下面是整个程序的源代码:
#include<stdio.h>
#include"1.h"
//是否透明可见
#define YES 1
#define NO 0
//边框线的样式
#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 BORDER_BLUE 0X800
#define BORDER_GREEN 0X400
#define BORDER_RED 0X200
#define BORDER_MASK 0xE00
#define B_SOLID 0
#define B_DOTTED 0X1000
#define B_DASHED 0X2000
#define STYLE_MASK 0X3000
const char *colors[8]={"black","red","green","yellow","blue","magenta","cyan","white"};
struct box_props{
    unsigned int opaque       :1;
    unsigned int fill_color   :3;
    unsigned int              :4;
    unsigned int show_border  :1;
    unsigned int border_color :2;
    unsigned int border_style :2;
    unsigned int              :2;
};
union Views
{
    struct box_props st_view;
    unsigned int ui_view;
};
void show_settings(const struct box_props *pb);
void show_settings1(unsigned short);
char *itobs(int n,char *ps);//把short值以二进制字符串的形式显示
int main(void)
{
    copycode("位字段运算符.txt");
    //创建views对象,初始化结构box view
    union Views box={{YES,YELLOW,YES,GREEN,DASHED}};
    char bin_str[8*sizeof(unsigned int)+1];
    printf("%d\n",box.ui_view);
    printf("original box settings:\n");
    show_settings(&box.st_view);
    printf("\nbox settings using unsigned int view:\n");
    show_settings1(box.ui_view);
    printf("bits are %s\n",itobs(box.ui_view,bin_str));
    box.ui_view&=~FILL_MASK;//把代表填充色的位清0
    box.ui_view|=(FILL_BLUE|FILL_GREEN);//重置填充色
    box.ui_view^=OPAQUE;//转置指示是否透明的位
    box.ui_view|=BORDER_RED;//错误的方法
    box.ui_view&=~STYLE_MASK;//清除样式位
    box.ui_view|=B_DOTTED;//把样式设置为点
    printf("\nmodified box settings:\n");
    show_settings(&box.st_view);
    printf("\nbox settings using unsigned int view:\n");
    show_settings1(box.ui_view);
    printf("bits are %s\n",itobs(box.ui_view,bin_str));
    return 0;
}
void show_settings(const struct box_props *pb)
{
    printf("box is %s\n",pb->opaque==YES?"opaque":"transparent");
    printf("The fill color is %s.\n",colors[pb->fill_color]);
    printf("Border %s.\n",pb->show_border==YES?"shown":"not shown");
    printf("the border color is %s.\n",colors[pb->border_color]);
    printf("the border style is ");
    switch(pb->border_style)
    {
        case SOLID:printf("solid.\n");break;
        case DOTTED:printf("dotted.\n");break;
        case DASHED:printf("dashed.\n");break;
        default:printf("unknown type.\n");
    }
}
void show_settings1(unsigned short us)
{
    printf("%d\n",us);
    printf("box is %s.\n",us&OPAQUE==OPAQUE?"opaque":"transparent");
    printf("the fill color is %s.\n",colors[(us>>1)&07]);
    printf("%d\n",us);
    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]);
}
char *itobs(int n,char *ps)
{
    int i;
    static int size=8*sizeof(unsigned int);
    for(i=size-1;i>=0;i--,n>>=1)
    ps[i]=(01&n)+'0';
    ps[size]='\0';
    return ps;
}
搜索更多相关主题的帖子: include C语言 联合 
2015-12-11 20:39
快速回复:关于C语言联合(union)的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020217 second(s), 11 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved