使用typedef和struct创建位域

时间:2013-03-24 04:00:40

标签: c structure bit-fields

嘿,这一切都是为了C编程。

以下是提交给我的问题,有人可以告诉我如何在C中正确编写以下语句的代码吗?仅供参考,我已经回答了这个测验,但我的教授不会自己发布答案。当他评分时我做得很差,但帮助理解这一点我将提供我的答案(尽管不正确)

1:创建一个包含以下代码的结构:

  • 计数:4位BCD号码
  • 红色LED:1位
  • 绿色LED:1位
  • 电机方向:2位

将结构命名为motorStatus

使用typedef命名此新数据类型:mtrStatus_t

typedef unsigned char mtrStatus_t;

struct motorStatus mtrStatus_t {
    mtrStatus_t count: 4;
    mtrStatus_t redLED: 1;
    mtrStatus_t greenLED: 1;
    mtrStatus_t motorDirection: 2;
};

2:创建结构的新实例并将其命名为motor1Status

motorStatus = motor1Status;

3:编写语句以初始化新结构成员,如下所示:

  • 数:9 BCD
  • 红色LED:1
  • 绿色LED:0
  • 电机方向:10

    计数:0x09; redLED:0x01; greenLED:0x00 motorDirection:0x0A

2 个答案:

答案 0 :(得分:2)

对于第一个,我会做这样的事情:

typedef struct motorStatus  
{
    int count: 4;
    int redLED: 1;
    int greenLED: 1;
    int motorDirection: 2;
} mtrStatus_t;

第二个更像是:

mtrStatus_t motor1status;

最后:

motor1status.count = 0x9;
motor1status.redLED = 1;
motor1status.greenLED = 0;
motor1status.motorDirection = 0x02;

Count是十六进制数,因为它是BCD(二进制编码的十进制)http://en.wikipedia.org/wiki/Binary-coded_decimal 在BCD中,使用4位来表示数字0-9,有一些未使用的位模式,因此使用它的简单方法是使用十六进制(也使用4位来表示数字0x0-0xf),但在BCD中你只是不使用数字0xa-0xf。

motorDirection为0x02的原因是因为他想要电机方向为10,但它是一个2位字段,所以我假设他的意思是10二进制,即0x02十六进制。

答案 1 :(得分:0)

考虑按照指定的顺序满足这些要求。请注意,某些要求合并为一个。作业通常就是这种情况;讲师并不总是完美的语言学家或逻辑学家,特别是IT讲师。

1: Create a structure that will hold bits the represent the following:

count: a 4 bit BCD number
red LED: 1 bit
green LED: 1 bit
motor direction: 2 bits

Name the structure motorStatus

首先回应这个,不使用typedef。对于位域使用类型int。下一个要求:

Use typedef to name this new data type: mtrStatus_t

您已经证明了编写基本typedef的能力。 typedef unsigned char mtrStatus_t;表示“我将mtrStatus_t定义为别名unsigned char”。现在,写一个像这样的基本typedef,意思是“我将mtrStatus_t定义为alias struct motorStatus”。将它放在struct motorStatus定义之后,以便编译器可以看到它的别名。

2: Create a new instance of the structure and name it motor1Status

为了澄清,您的讲师要求您声明名为motor1Status的变量,其类型为mtrStatus_tstruct motorStatus。我认为你可以声明变量,但如果我错了就纠正我。

3: Write statements to initialize the new structure members as follows:

count: 9 BCD
red LED: 1
green LED: 0
Motor Direction: 10

count: 0x09; redLED: 0x01; greenLED: 0x00 motorDirection: 0x0A

您的讲师要求初始化,未分配。在char str[] = "fubar";char str[] = {'h','e','l','l','o','\0'};中,声明并初始化str以存储对应于“fubar”的字符串。在char str[6]; strcpy(str, "fubar");中声明str没有初始化,并且“fubar”中的每个字节被复制(分配)到str中的相应位置。你如何初始化结构?与第二个 str 初始化非常相似。

struct length_prefixed_string {
    size_t length;
    int zero;
    char string[];
};

/* Initialisation here: */
struct length_prefixed_string hello = { .length = 5,
                                        .string = "hello" };

此示例使用灵活的数组成员,该成员只能在结构的末尾声明:char string[];。它传达一个位于结构末尾的未知长度数组,不计入sizeof (struct length_prefix_string)。因此,该示例将未知长度存储在 length 参数中。这有点偏离主题,但你可以看到上面的初始化(而不是作业)。这个例子将初始化与赋值区分开来;您无法使用.string分配此结构的hello.string = "hello";成员。

另一个区别是:初始化时未提及的任何成员将被分配值(但在初始化时),因此{{上面示例中的1}}将为0.因此,您可以声明一个数组并使用一个语句对其进行零填充:hello.zero(赋值和初始化之间的另一个区别:int array[size] = { 0 };无效) 。话虽如此,为了你的任务而忽略这个事实并明确地初始化greenLED是一个好主意,这样你的标记审查员就不会误解。

我希望我有所帮助,这里......

相关问题