定义结构中字段的冒号(:)语法 [英] Colon (:) syntax in defining fields in a struct
问题描述
大家好,
我最近遇到了以下代码段,它定义了一个C
结构:
typedef struct
{
unsigned char unused_bits:4;
unsigned char wchair_state:2;
} xyz;
数字4和2指的是什么?
如果我定义第二个结构如下:
typedef struct
{
unsigned char unused_bits;
unsigned char wchair_state;
} abc;
然后声明
void main(无效)
{
xyz _xyz;
abc _abc;
}
在内存分配方面,它是否有任何区别
分配给_xyz和_abc?
任何反馈都会非常感激。
谢谢
Raj
":"表示比特分配。
系统将为struct _xyz分配1个字节,为struct _abc分配2个字节。
这是mem结构图:
_________________________________________________
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
---------------------------------------- ---------
|< - _xyz.u_bits - > |< - _xyz.w-> |
_________________
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | < ---- _abc.u_bits;
+ - + - + - + - + - + - + - + - +
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | < ---- _abc.w
-----------------
那就是,并且有效。
我真的希望你能得到它。
-
Shark Vanue
< blockquote> ra********@hotmail.com (Raj Kotaru)写道:
#Hello all,
#
#我最近遇到了以下代码段,它定义了一个C $ / $
#struct :
#
#typedef struct
#{
#unsigned char unused_bits:4;
#unsigned char wchair_state:2;
#} xyz;
#
#数字4和2是指什么?
unused_bits是四位宽,wchair_state是两位。字段可能会尽可能紧密地打包在一个字符大小的单元中。
在典型的CPU和C实现中,没有字段宽度,
结构将是两个字符宽,只有一个字符宽,上面是
。
#
#如果我定义第二个结构如下:
#
#typedef struct
#{
#unsigned char unused_bits;
#unsigned char wchair_state;
#} abc;
#
#
#然后声明
#
#void main(void)
#{
#xyz _xyz;
#abc _abc;
#}
#
#就...而言内存分配,
#分配给_xyz和_abc之间有什么区别吗?
添加
printf("%) d%d \ n",sizeof(xyz),sizeof(abc));
我希望看到它打印
1 2
- -
SM Ryan http://www.rawbw.com / ~wyrmwif /
成为烈士的一个缺点就是你必须死。
ra ******** @ hotmail.com (Raj Kotaru)写道:I最近遇到了以下定义C />结构的代码段:
typedef struct
{unsigned char unused_bits:4;
unsigned char wchair_state :2;
} xyz;
查找位字段在任何C教科书中。
-
Keith Thompson(The_Other_Keith) ks ***@mib.org < http://www.ghoti.net/~kst>
圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>
我们必须做点什么。这是事情。因此,我们必须这样做。
Hello all,
I recently came across the following segment of code that defines a C
struct:
typedef struct
{
unsigned char unused_bits:4;
unsigned char wchair_state:2;
} xyz;
What do the numbers 4 and 2 refer to?
If I define a second struct as below:
typedef struct
{
unsigned char unused_bits;
unsigned char wchair_state;
} abc;
and then declare
void main(void)
{
xyz _xyz;
abc _abc;
}
In terms of memory allocation, is there any difference between that
allocated for _xyz and _abc?
Any feedback would be much appreciated.
Thanks
Raj
":" means bit-allocation.
System will allocate 1 byte to struct _xyz and 2 byte to struct _abc.
Here is mem structure chart:
_________________________________________________
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
-------------------------------------------------
|<- _xyz.u_bits ->|<- _xyz.w->|
_________________
|1|2|3|4|5|6|7|8| <---- _abc.u_bits;
+-+-+-+-+-+-+-+-+
|1|2|3|4|5|6|7|8| <---- _abc.w
-----------------
That is,and works .
I really want you get it.
--
Shark Vanue
ra********@hotmail.com (Raj Kotaru) wrote:
# Hello all,
#
# I recently came across the following segment of code that defines a C
# struct:
#
# typedef struct
# {
# unsigned char unused_bits:4;
# unsigned char wchair_state:2;
# } xyz;
#
# What do the numbers 4 and 2 refer to?
unused_bits is four bits wide and wchair_state is two bits. The fields may
be packed as tightly as possible, in one char sized unit possibly.
On a typical CPU and C implementation, without the field widths, the
struct would be two characters wide, and only one character wide with
the above.
#
# If I define a second struct as below:
#
# typedef struct
# {
# unsigned char unused_bits;
# unsigned char wchair_state;
# } abc;
#
#
# and then declare
#
# void main(void)
# {
# xyz _xyz;
# abc _abc;
# }
#
# In terms of memory allocation, is there any difference between that
# allocated for _xyz and _abc?
Add
printf("%d %d\n",sizeof(xyz),sizeof(abc));
I would expect to see it print
1 2
--
SM Ryan http://www.rawbw.com/~wyrmwif/
One of the drawbacks of being a martyr is that you have to die.
ra********@hotmail.com (Raj Kotaru) writes:I recently came across the following segment of code that defines a C
struct:
typedef struct
{
unsigned char unused_bits:4;
unsigned char wchair_state:2;
} xyz;
Look up "bit fields" in any C textbook.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
这篇关于定义结构中字段的冒号(:)语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!