Chinaunix首页 | 论坛 | 博客
  • 博客访问: 29919
  • 博文数量: 3
  • 博客积分: 276
  • 博客等级: 入伍新兵
  • 技术积分: 45
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-21 13:32
文章分类
文章存档

2011年(3)

分类: C/C++

2011-10-23 00:01:20

经常在CU看各位大牛的文章,就目前自己的情况来说,他们的水平对我来说只能是可望而不可及。在此发文,权当巩固每天所学或者不经意间了解的东西,希望自己好好学习,进步快点。若哪里说的不对,请各位指出,以免误导别人,自己也好长点知识。
最近看linux内核和light sensor driver的源码,其中出现最多的应该就是结构体了。可是它的结构体初试化感觉和以前所看的不太一样(本科专业是电子,C语言只看过谭的那本),并没有花时间细抠“为什么在每个成员前面加一个‘.’?”之类的小问题。今天起床无事,突然想起来,便随便查了一下,现在整理下来供自己和正在入门的兄弟姐妹参考。

如light sensor driver里面有如下结构体:
  1. struct i2c_driver {
  2.     unsigned int class;

  3.     /* Notifies the driver that a new bus has appeared or is about to be
  4.      * removed. You should avoid using this if you can, it will probably
  5.      * be removed in a near future.
  6.      */
  7.     int (*attach_adapter)(struct i2c_adapter *);
  8.     int (*detach_adapter)(struct i2c_adapter *);

  9.     /* Standard driver model interfaces */
  10.     int (*probe)(struct i2c_client *, const struct i2c_device_id *);
  11.     int (*remove)(struct i2c_client *);

  12.     /* driver model interfaces that don't relate to enumeration */
  13.     void (*shutdown)(struct i2c_client *);
  14.     int (*suspend)(struct i2c_client *, pm_message_t mesg);
  15.     int (*resume)(struct i2c_client *);

  16.     /* Alert callback, for example for the SMBus alert protocol.
  17.      * The format and meaning of the data value depends on the protocol.
  18.      * For the SMBus alert protocol, there is a single bit of data passed
  19.      * as the alert response's low bit ("event flag").
  20.      */
  21.     void (*alert)(struct i2c_client *, unsigned int data);

  22.     /* a ioctl like command that can be used to perform specific functions
  23.      * with the device.
  24.      */
  25.     int (*command)(struct i2c_client *client, unsigned int cmd, void *arg);

  26.     struct device_driver driver;
  27.     const struct i2c_device_id *id_table;

  28.     /* Device detection callback for automatic device creation */
  29.     int (*detect)(struct i2c_client *, struct i2c_board_info *);
  30.     const unsigned short *address_list;
  31.     struct list_head clients;
  32. };
  1. struct i2c_driver al3000_driver = {
  2.     .driver = {
  3.         .name    = AL3000_DRV_NAME,
  4.         .owner    = THIS_MODULE,
  5.     },
  6.     .suspend = al3000_suspend,
  7.     .resume    = al3000_resume,
  8.     .probe    = al3000_probe,
  9.     .command = al3000_command,
  10.     .remove    = __devexit_p(al3000_remove),
  11.     .id_table = al3000_id,
  12. };
这种初始化写法就是所谓的C语言标记化结构初始化语法。
采用 .name = value这种形式,虽然没什么,但带来许多好处。举个小例子:
  1. struct student { //结构体名
  2.     char name[20];
  3.     char s;
  4.     int age;
  5.     int score;
  6. };
  1. struct student stu1 ={ //传统初始化
  2.     "x",'m',1,2
  3. };
  4. struct student stu1 ={ //上述标记式初始化
  5.     .score=2,
  6.     .age=1,
  7.     .name="x",
  8. };
通俗总结一下优点:
1.顺序。(不理会结构体成员顺序,允许对结构成员进行重新排列。)
2.个数。(可以有选择性的初始化,不需要针对所有成员都进行初始化。)
3.性能。(将频繁被访问的成员放在相同的硬件缓存行上,将大大提高性能。)
4.扩展性好。(如增加字段时,避免了传统的那种大量修改。)
阅读(3526) | 评论(3) | 转发(0) |
给主人留下些什么吧!~~

greenlasting2012-12-24 21:37:12

不明觉历,你怎么找到我的

xhkai02152011-10-24 19:28:57

GFree_Wind: 帮你补充一下,这种初始化方法为GNU的扩展特性.....
嗯 ,谢了

GFree_Wind2011-10-24 11:51:05

帮你补充一下,这种初始化方法为GNU的扩展特性