首页 > 其他 > 详细

C和指针 第十章 结构和联合 习题

时间:2016-09-02 23:07:36      阅读:111      评论:0      收藏:0      [点我收藏+]

1. 记账信息结构联合

typedef struct {
    unsigned int areaNum;
    unsigned int transNum;
    unsigned int station;
} phoneNumber;

struct {
    phoneNumber fromPhone;
    phoneNumber toPhone;
    phoneNumber payPhone;
} dialHistory;

2. 指令解码:

#include <stdio.h>

typedef struct {
    unsigned int dsg_reg : 3;
    unsigned int dst_mode: 2;
    unsigned int opcode  : 11;
} sgl;

typedef struct {
    unsigned int dst_reg : 3;
    unsigned int dst_mode: 3;
    unsigned int src_reg : 3;
    unsigned int src_mode: 3;
    unsigned int opcode  : 4;
} dbl;

typedef struct {
    unsigned int offset : 8;
    unsigned int opcode : 8;
} bran;

typedef struct {
    unsigned int dst_reg : 3;
    unsigned int dst_mode : 3;
    unsigned int src_reg : 3;
    unsigned int opcode : 7;
} reg;

typedef struct {
    unsigned int opcode : 16;
} mic;

typedef union {
    	unsigned short addr;
        mic misc;
        bran branch;
        sgl sgl_op;
        reg reg_src;
        dbl dbl_op;
} machine_inst ;

int main()
{
    machine_inst x = {16};

    printf("%0x\n", x.sgl_op.dst_mode);
    return 0;
}

  typedef 定义联合类型,就可以使用各种联合内的字段结构,访问了

C和指针 第十章 结构和联合 习题

原文:http://www.cnblogs.com/yangxunwu1992/p/5835744.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!