What is tag structure initialization syntax?

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 initialization tag structure syntax.

Can someone clarify?

+39
c syntax linux-kernel
Jun 10 2018-10-10
source share
3 answers

When you use aggregate initializers (initializers in {} ) in the "traditional" ANSI C language (C89 / 90), you must specify an individual initializer for each member of the structure in the order starting from the first. for example

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

You do not have to specify initializers for all members, i.e. you can stop at any time (other members will be initialized to zeros).

If for some reason you just needed to explicitly initialize the third element of the structure, you had to provide explicit "dummy" initializers for the first and second members (just to get the right third)

 /* We only care to explicitly initialize `sc` */ struct S s = { 0, 0, 3 }; /* but we have to explicitly initialize `sa` and `sb` as well */ 

or completely abandon a specific initialization (most likely, replacing it with general = { 0 } ) and use the subsequent assignment to certain members

 struct S s = { 0 }; sc = 3; 

One of the notable advantages of this assignment-based approach is that it is independent of the position of the c member in the struct S declaration.

The new C language specification (C99) allows you to use initializers with a label by placing the desired member name in {}

 struct S s = { .c = 3 }; 

Thus, you only explicitly initialize the required member (and have a compiler for zero initialization of the rest). This not only saves you from typing, but also makes aggregate initializers independent of the order in which members are specified in the structure type declaration.

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

 int a[10] = { [5] = 3 }; /* `a[5]` is initialized with 3, the rest of `a` is zero-initialized */ 

It is worth noting once again that the C language continues to adhere to the all-or-nothing approach for aggregation initialization: if you specify an explicit initializer for only one (or some) elements of a structure or array, the whole set is initialized, and members without explicit initializers get zero-initialization .

+67
Jun 10 2018-10-10
source share
— -

You use structure member names to initialize the structure. that is, the initialization of each member is “tagged” with that member name.

+4
Jun 10 2018-10-10
source share

Another advantage worth mentioning with respect to this type of initialization is that it allows reordering of members of the structure, which in some cases can improve performance, for example, by placing pointers to frequently used elements in the same line of the equipment cache.

+1
Oct. 16 '17 at 11:06 on
source share



All Articles