c - 这些 struct 和 typedef 定义是什么意思?

标签 c struct typedef

我有以下代码,它工作正常并且已经使用了很长一段时间......但我不知道它意味着什么。

struct event_param
{
    int task:3;
    int param1;
    int param2;
};

#define SV_DRIVER_EVENTS_MASK_SIZE (SV_DRIVER_EVENT_LAST*sizeof(struct event_param))
typedef struct event_param driver_event_mask[SV_DRIVER_EVENTS_MASK_SIZE];
typedef driver_event_mask DriverEventMask;
  • driver_event_mask 代表什么?
  • 为什么数组中有 sizeof(struct event_param)

最佳答案

:3 代表 bit field 。不过,我不确定这有多重要,因为我认为 sizeof(struct event_param) 几乎肯定仍然是 12 个字节。

所以这里发生的是创建 event_param 结构的 SV_DRIVER_EVENT_LAST 数组。但我也希望 sizeof(struct event_param) 在这里不是必需的——数组的长度是用它包含的单位数来定义的,而不是用字节来定义。

1. driver_event_mask 就是它所说的。表示 SV_DRIVER_EVENTS_MASK_SIZE struct event_param 项数组的类型。

2. sizeof(struct event_param) 在这里不需要,但不会造成伤害(即不会导致错误),因为它只意味着您分配大约 12倍于您实际需要的内存。

关于c - 这些 struct 和 typedef 定义是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12192806/

相关文章:

python - 从 Python 到 C, float 的精度是否发生变化?

c - 如果从 EXCEL 复制数据,strtok 不起作用

c++ - 模板类的typedef

c - 在链接列表中输入数据

在 C 中用 long double 计算 pi

c - 在指向结构的指针数组中混合指针的取消引用形式

swift - SDWebImage 和 Swift

c - 如何在c中使用struct获取动态数组

c - C 中的代码不清楚,如果有人向我解释,我将不胜感激

c - ':' token 之前应为 ',' 、 ';' 、 '}' 、 '__attribute__' 或 '='