“:”(冒号)在 C 结构体中 - 它是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8564532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
":" (colon) in C struct - what does it mean?
提问by Mohamed Maged
struct _USBCHECK_FLAGS
{
unsigned char DEVICE_DEFAULT_STATE : 1;
unsigned char DEVICE_ADDRESS_STATE : 1;
unsigned char DEVICE_CONFIGURATION_STATE : 1;
unsigned char DEVICE_INTERFACE_STATE : 1;
unsigned char FOUR_RESERVED_BITS : 8;
unsigned char RESET_BITS : 8;
} State_bits;
What does :1
and :8
mean?
是什么:1
和:8
意味着什么?
采纳答案by JoeFish
Those are bit fields. Basically, the number after the colon describes how many bits that field uses. Here is a quote from MSDNdescribing bit fields:
这些是位域。基本上,冒号后面的数字描述了该字段使用的位数。这是MSDN 中描述位域的引述:
The constant-expression specifies the width of the field in bits. The type-specifier for the declarator must be unsigned int, signed int, or int, and the constant-expression must be a nonnegative integer value. If the value is zero, the declaration has no declarator. Arrays of bit fields, pointers to bit fields, and functions returning bit fields are not allowed. The optional declarator names the bit field. Bit fields can only be declared as part of a structure. The address-of operator (&) cannot be applied to bit-field components.
Unnamed bit fields cannot be referenced, and their contents at run time are unpredictable. They can be used as "dummy" fields, for alignment purposes. An unnamed bit field whose width is specified as 0 guarantees that storage for the member following it in the struct-declaration-list begins on an int boundary.
This example defines a two-dimensional array of structures named screen.
常量表达式以位为单位指定字段的宽度。声明符的类型说明符必须是 unsigned int、signed int 或 int,并且常量表达式必须是非负整数值。如果值为零,则声明没有声明符。不允许使用位域数组、指向位域的指针和返回位域的函数。可选的声明符命名位域。位域只能声明为结构的一部分。地址运算符 (&) 不能应用于位域组件。
无法引用未命名的位域,并且它们在运行时的内容是不可预测的。它们可以用作“虚拟”字段,用于对齐目的。宽度被指定为 0 的未命名位域保证结构声明列表中紧随其后的成员的存储开始于 int 边界。
这个例子定义了一个名为 screen 的二维结构数组。
struct
{
unsigned short icon : 8;
unsigned short color : 4;
unsigned short underline : 1;
unsigned short blink : 1;
} screen[25][80];
Edit: another important bit from the MSDN link:
编辑:来自 MSDN 链接的另一个重要部分:
Bit fields have the same semantics as the integer type. This means a bit field is used in expressions in exactly the same way as a variable of the same base type would be used, regardless of how many bits are in the bit field.
位字段与整数类型具有相同的语义。这意味着在表达式中使用位域的方式与使用相同基本类型的变量的方式完全相同,而不管位域中有多少位。
A quick sample illustrates this nicely. Interestingly, with mixed types the compiler seems to default to sizeof (int)
.
一个快速示例很好地说明了这一点。有趣的是,对于混合类型,编译器似乎默认为sizeof (int)
.
struct
{
int a : 4;
int b : 13;
int c : 1;
} test1;
struct
{
short a : 4;
short b : 3;
} test2;
struct
{
char a : 4;
char b : 3;
} test3;
struct
{
char a : 4;
short b : 3;
} test4;
printf("test1: %d\ntest2: %d\ntest3: %d\ntest4: %d\n", sizeof(test1), sizeof(test2), sizeof(test3), sizeof(test4));
test1: 4
test2: 2
test3: 1
test4: 4
测试 1:4
测试2:2
测试3:1
测试 4:4
回答by ykuksenko
I also ran into the colon notation but in my context bit fields didn't make sense. So I did some digging. This notation is also used for assigning values - in my specific situation pointers to functions.
我也遇到了冒号符号,但在我的上下文中位域没有意义。所以我做了一些挖掘。这个符号也用于赋值——在我的特定情况下是指向函数的指针。
Source: http://www.tldp.org/LDP/lkmpg/2.4/html/c577.htm
资料来源:http: //www.tldp.org/LDP/lkmpg/2.4/html/c577.htm
Below is a sample and an excerpt to explain.
下面是一个示例和摘录来解释。
"There is a gcc extension that makes assigning to this structure more convenient. You'll see it in modern drivers, and may catch you by surprise. This is what the new way of assigning to the structure looks like:"
"有一个 gcc 扩展可以更方便地分配给这个结构。你会在现代驱动程序中看到它,并且可能会让你感到惊讶。这是分配给结构的新方法的样子:"
struct file_operations fops = {
read: device_read,
write: device_write,
open: device_open,
release: device_release
};
The C99 (old, compatible) way looks like:
C99(旧的,兼容的)方式看起来像:
struct file_operations fops = {
.read = device_read,
.write = device_write,
.open = device_open,
.release = device_release
};
回答by zvrba
It defines bit-fields of width 1 and 8.
它定义了宽度为 1 和 8 的位域。