2008-06-16 11:00 在设备驱动中常见下面这种语法格式,首先定义一堆函数指针,并根据不同的设备初始化为不同的实现函数,很像 C++ 里面的成员函数。
struct file_operations scull_fops = {
.owner = THIS_MODULE,
.llseek = scull_llseek,
.read = scull_read,
.write = scull_write,
.ioctl = scull_ioctl,
.open = scull_open,
.release = scull_release,
};
这种语法使用了指派初始化(designated_initializers ),使程序可以使不同的结构定义间的移植更加容易,代码更加精简易读。而且编译器可以根据需要改变结构成员的顺序,提升程序的性能。
以下内容摘自 gcc 手册
In a structure initializer, specify the name of a field to initialize with `.fieldname =' before the element value. For example, given the following structure,
struct point {int x, y;};
The following initialization
struct point p = {
.y = yvalue,
.x = xvalue
};
is equivalent to
struct point p = {xvalue, yvalue};