首页 > 代码库 > C和指针 第十章 结构和联合 习题

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

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和指针 第十章 结构和联合 习题