本章重點:結構體嵌套、位域、對齊與內存佈局。
typedef struct {
unsigned char QuHao[5]; // 區號
unsigned char Exchange[10]; // 局號
unsigned char StnNum[10]; // 用戶號
} TelphoneNumber;
typedef struct {
unsigned char date[10];
unsigned char time[10];
TelphoneNumber UserTelphone;
TelphoneNumber CallTelphone;
TelphoneNumber PayTelphone;
} Telphone_Call;
typedef struct { float MSRP, ActualPrice, SalesTax, LicenseFee; } CashSale;
typedef struct { float MSRP, ActualPrice, DownPayment, SecurityDeposit, MonthlyPayment; int LeaseTerm; } Rent;
typedef struct { float MSRP, ActualPrice, SalesTax, LicenseFee, DownPayment; int LoanDuration; float InterestRate, MonthlyPayment; char BankName[21]; } LoanSale;
typedef struct {
char CustomerName[21], CustomerAddr[41], Model[21];
CashSale CashSal;
Rent RentSal;
LoanSale LoanSal;
} SaleRecord;
typedef struct { unsigned int dst_reg:3, dst_mode:3, opcode:10; } SingleOperat;
typedef struct { unsigned int dst_reg:3, dst_mode:3, src_reg:3, src_mode:3, opcode:4; } DoubleOperat;
typedef struct { unsigned int offset:8, opcode:8; } Branch;
typedef struct { unsigned int dst_reg:3, dst_mode:3, src_reg:3, opcode:7; } RegistSrc;
typedef struct { unsigned int opcode:16; } MiscCmd;
typedef struct {
unsigned short addr;
SingleOperat sgl_op;
DoubleOperat dbl_op;
Branch branch;
RegistSrc reg_src;
MiscCmd misc;
} machine_inst;
位域自動打包進整數單元,順序視實現定義(通常從低位到高位),跨平台移植需注意端序與填充規則。