在 C 语言中,':' 是什么意思?做?
可能的重复:
“unsigned temp:3”是什么意思
我一直在尝试学习原始套接字编程在 C 中,遇到了这个:
unsigned char iph_ihl:5, iph_ver:4;
我对 ':' 的作用感到困惑。它甚至有什么作用吗?或者它只是变量名称的一部分?
Possible Duplicate:
What does 'unsigned temp:3' means
I have been trying to learn raw socket programming in C and have come across this:
unsigned char iph_ihl:5, iph_ver:4;
I am confused about what the ':' does. Does it even do anything? Or is it just part of the variable's name?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在查看位域。这些定义必须位于结构内部,它们意味着 iph_ihl 是 5 位字段,iph_ver 是 4 位字段。
您的示例有点奇怪,因为
unsigned char
在大多数机器上都是 8 位类型,但其中声明了 9 位字段。一般来说,位域是不可移植的,所以我建议不要使用它们,但您可以阅读有关它们的更多信息 这里。
You're looking at bitfields. Those definitions have to be inside a structure, and they mean that
iph_ihl
is a 5-bit field andiph_ver
is a 4-bit field.Your example is a bit strange, since an
unsigned char
would be an 8-bit type on most machines, but there are 9 bits worth of fields declared there.In general bitfields are pretty non-portable, so I would recommend against their use, but you can read more about them here.
它是位字段。请参阅这个有关 C 位字段的良好文档。 .它通常用于内存受限的情况(例如嵌入式编程),以紧密地包装我们的使用。.
重要的一点位字段没有地址 - 你不能有指向它们的指针或它们的数组
It is bit fields..See this good documentation about C bit fields..It is normally used in memory constrained situations (example embedded programming), to tightly pack our usage..
Important point Bit fields do not have addresses—you can't have pointers to them or arrays of them
除了上面提到的答案之外,您还可以查看此,以获取有关位字段的详细介绍。需要注意的一件事:
c
中的位字段只能用于integer
类型。使用位字段不仅会使您的程序不可移植
,而且还会依赖于编译器
。Apart from the above mentioned answers, you can take a look at this for a good introduction on bit fields. One thing to note: bit fields in
c
can be used only oninteger
types. Using bit fields will not only make your program benon-portable
, it will also becompiler-dependent
.