views:

161

answers:

2
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,
};

This declaration uses the standard C tagged structure initialization syntax.

Can someone elaborate?

+2  A: 

You're using the names of the members of the structure to initialise the structure. i.e. each member initialisation is "tagged" with that member's name.

Graham Lee
+10  A: 

When you use aggregate initializers (initializers in {}) in the "traditional" ANSI C language (C89/90), you have to supply an individual initializer for each structure member in order, beginning with the first. For example

struct S { int a, b, c, d; };

struct S s = { 1, 2, 3, 4 };
/* 1 for `s.a`, 2 for `s.b` and so on... */

You are not required to specify initializers for all members, i.e. you can stop at any time (remaining members will be zero-initialized). If for some reason you only cared to explicitly initialize the third member of the structure, you had no other choice but to supply a "dummy" explicit initializer for the first and the second members (just to get to the desired third)

/* We only care to explicitly initialize `s.c` */
struct S s = { 0, 0, 3 };
/* and we have no choice but to explicitly initialize `s.a` and `s.b` as well */

The new specification of C language (C99) allows you to use "tagged" initializers by supplying the desired member name within the {}

struct S s = { .c = 3 };

That way you only explicitly initialize the desired member(s) (and ask the compiler to implicitly zero-initialize the rest).

This not only saves you some typing but also makes the aggregate initializers independent from the order in which the members are declared in the struct.

Aggregate initializers, as you probably know, can be used with arrays, too. And C99 supports "tagged" initialization with arrays as well. How the "tags" look in case of an array is illustrated by the following example

int a[10] = { [5] = 3 };

It is worth noting one more time that C language continues to stick to the "all or nothing" approach to aggregate initialization: if you specify an explicit initializer for just one (or some) members of a struct or an array, the whole aggregate (struct or array) gets initialized, and the members without explicit initializers get zero-initialized.

AndreyT