初学c语言开发的小伙伴们,在学习的过程中,可能会发现在有些结构体定义里的变量定义后面出现冒号跟着数字的情况,例如下面这个结构体的定义,

struct ngx_event_s {

void            *data;

unsigned         write:1;

unsigned         accept:1;

/* used to detect the stale events in kqueue and epoll */

unsigned         instance:1;

/*

* the event was passed or would be passed to a kernel;

* in aio mode - operation was posted.

*/

unsigned         active:1;

unsigned         disabled:1;

/* the ready event; in aio mode 0 means that no operation can be posted */

unsigned         ready:1;

unsigned         oneshot:1;

/* aio operation is complete */

unsigned         complete:1;

unsigned         eof:1;

unsigned         error:1;

unsigned         timedout:1;

unsigned         timer_set:1;

unsigned         delayed:1;

unsigned         deferred_accept:1;

/* the pending eof reported by kqueue, epoll or in aio chain operation */

unsigned         pending_eof:1;

unsigned         posted:1;

unsigned         closed:1;

/* to test on worker exit */

unsigned         channel:1;

unsigned         resolver:1;

unsigned         cancelable:1;

#if (NGX_HAVE_KQUEUE)

unsigned         kq_vnode:1;

/* the pending errno reported by kqueue */

int              kq_errno;

#endif

/*

* kqueue only:

*   accept:     number of sockets that wait to be accepted

*   read:       bytes to read when event is ready

*               or lowat when event is set with NGX_LOWAT_EVENT flag

*   write:      available space in buffer when event is ready

*               or lowat when event is set with NGX_LOWAT_EVENT flag

*

* iocp: TODO

*

* otherwise:

*   accept:     1 if accept many, 0 otherwise

*   read:       bytes to read when event is ready, -1 if not known

*/

int              available;

ngx_event_handler_pt  handler;

#if (NGX_HAVE_IOCP)

ngx_event_ovlp_t ovlp;

#endif

ngx_uint_t       index;

ngx_log_t       *log;

ngx_rbtree_node_t   timer;

/* the posted queue */

ngx_queue_t      queue;

#if 0

/* the threads support */

/*

* the event thread context, we store it here

* if $(CC) does not understand __thread declaration

* and pthread_getspecific() is too costly

*/

void            *thr_ctx;

#if (NGX_EVENT_T_PADDING)

/* event should not cross cache line in SMP */

uint32_t         padding[NGX_EVENT_T_PADDING];

#endif

#endif

};

当你第一次见到这种情况,于是一脑门子的问号就浮现出来了。。。

这是什么东东,我怎么从来没见过呢,为什么用这个东东。。。

我们一个一个来看,

首先,我们认识一下这是个什么东东。英文中,称结构体内这种有预定义宽度类型的成员变量为 bit fields ,翻译过来,称位域。一个位域可以不只占一个 bit 位,譬如,你需要一个存储值范围为0 到 7 的变量,那么你需要定义 3 bit 位宽度的位域

struct {unsigned int age : 3;
} Age;

由此,我们看一下一个位域的声明(Bit Field Declaration),

struct {type [member_name] : width ;
};

可以看到,一个位域变量元素,包括,

        type ,位域的类型可以是, int, signed int, 或者 unsigned int ;

        member_name ,位域名;

width ,位域中的位数宽度必须小于或等于指定类型的位宽。

那为何要用这种东东呢?节省存储空间。想象一下,你需要构造一个结构体,其内包含的成员,只需要少量的 bit 位,就可以满足对事物的描述。像这样的情况,采用位域可以相对节省不少的存储空间。

c语言中结构体定义中的“冒号”相关推荐

  1. C#中结构体定义并转换字节数组

    ref: https://www.cnblogs.com/dafanjoy/p/7818126.html C#中结构体定义并转换字节数组 最近的项目在做socket通信报文解析的时候,用到了结构体与字 ...

  2. 【C语言】结构体定义 typedef struct 用法详解和用法小结

    结构体定义 typedef struct 用法详解和用法小结 文章目录 结构体定义 typedef struct 用法详解和用法小结 0. 前言 1. 首先:在C中定义一个结构体类型要用typedef ...

  3. java解析c语言的结构体,JAVA中如何实现C中的结构体数组的功能?

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 比如我想实现在C++中的结构体数组: struct student{ string Sno; string Sname; string Sgen; str ...

  4. c语言程序结构体排序,如何用C语言的结构体数组中的某一值排序?

    [C] 纯文本查看 复制代码#include "stdio.h" #include "string.h" struct student { char name[ ...

  5. c语言实现结构体变量private,C语言中结构体变量私有化详解

    背景介绍 操作系统 : CentOS7.3.1611_x64 gcc版本 :4.8.5 什么是结构体? 在C语言中,结构体(struct)指的是一种数据结构,是C语言中聚合数据类型(aggregate ...

  6. c语言结构体成员变量私有化,C语言中结构体变量私有化详解

    C语言中结构体变量私有化详解 背景介绍 操作系统 : CentOS7.3.1611_x64 gcc版本 :4.8.5 什么是结构体? 在C语言中,结构体(struct)指的是一种数据结构,是C语言中聚 ...

  7. Rust中结构体的定义和实例化

    文章目录 使用字段初始化简写语法 使用结构体更新语法从其他实例创建实例 使用没有命名字段的元组结构体来创建不同的类型 没有任何字段的类单元结构体 结构体和我们在"元组类型"部分论过 ...

  8. 用JAVA定义两个结构体_c语言struct结构体的定义和使用

    c语言由于不像java,c#,c++等语言有对象,所以就用struct结构体来表示,其实作用是差不多的,下面来快速学习c语言struct结构体的定义和使用,以学生类student来举例,有三种定义方式 ...

  9. 一起学习C语言:结构体(二)

    上一篇<一起学习C语言:结构体(一)> 中,我们了解了结构体的概念与定义形式,以及结构体变量初始化赋值.本章节,我们分析结构体成员访问形式,以及结构数组使用的场景. 章节预览: 4. 结构 ...

  10. C语言定义了一个结构体怎么分配内存?C\C++中结构体变量与结构体指针内存分配问题?

    C语言定义了一个结构体怎么分配内存?C\C++中结构体变量与结构体指针内存分配问题? 问题1:结构体指针最开始怎么分配内存?用sizeof()是不能确定大小的. 问题2:给结构体变量分配之后,是否还要 ...

最新文章

  1. 自定义mac的ll命令
  2. 【前端词典】进阶必备的网络基础(下)
  3. FFmpeg编解码处理1-转码全流程简介
  4. decode encode
  5. 运行在Docker里的SpringBoot应用,如何查看记录在文件系统的日志
  6. sqlserver 库服务器导数据
  7. int0低电平触发c语言,单片机中断的解释.ppt
  8. 浅谈Base64编码[转]
  9. 减一天 日期函数_excel日期函数:如何计算项目的开始和完成日期
  10. ArrayList 面试10连问
  11. 电视领域“烧钱”不止,酷开为何反成赢家?
  12. 在 UIWebView 中如何准确获得页面加载完成的事件
  13. 2018全国大学生电子竞赛D题代码和心得
  14. 如何将PPT输出为高精度的图片
  15. 白色/黄色/开关型/罗丹明B染料标记希夫碱/半胱氨酸乙酯荧光探针的制备过程
  16. 广发证券基于分布式架构的新一代估值系统实践
  17. 使当前线程暂停的方法
  18. 去除字符串前面的几个逗号
  19. SQL Server查询优化方法
  20. 100集华为HCIE安全培训视频教材整理 | 目的NAT及服务器负载均衡技术

热门文章

  1. B站压片 码率标准 和 码率变化 记录
  2. mapper扫描问题(Invalid bound statement (not found))
  3. Guava Joiner
  4. Linux下定时备份数据库
  5. Unity Sprite 灰色图
  6. 卡方检验python程序_Python卡方检验
  7. 苹果涨价就衰,iPhoneSE3再次证明这一规律
  8. VB长度宽度高度单位,width,scaleWidth,Height,scaleHeight,scaleMode
  9. 编译原理实验二:词法分析程序
  10. SRE实战手册-基础篇