首页 > 解决方案 > 在 C 中的 typedef 中定义变量的位/字节解释

问题描述

我是 C 编程的初学者。我得到了一个表格,其中的输入定义如下:

fieldA 8 bits
fieldB 5 bits
fieldc 2 bytes
fieldf 6 bits

所有这些字段都是输入配置的一部分。哪一个是为上述定义结构的更好方法?

typedef struct {
   unsigned fieldA: 8;  //used char here since inputs can be in hex format (eg 0x45)
   unsigned fieldb: 5;
   unsigned fieldc: 16;
   unsigned fieldf: 6;
} input_cfg;

struct input_cfg {
   unsigned char fieldA;  //used char here since inputs can be in hex format (eg 0x45)
   unsigned char fieldb;
   unsigned char fieldc;
   unsigned char fieldf;
};

提前非常感谢!

sksp

标签: c

解决方案


推荐阅读