Tag initialization: The first time I saw this term, although it was used countless times, I finally found a theoretical explanation.
In standard C (c89), the standard initialization of the structure is implemented using {}. In c99, more readable marking Initialization is adopted, this is common in linux kernels and drivers.
This is ISO c99 usage
C primer plus:
A known structure is defined as follows:
Struct book {
Char title [maxtitl];
Char author [maxautl];
Float value;
};
C99 supports the specified initialization project of the structure. Its syntax is similar to that of the specified initialization project of the array. However, the specified initialization project of the structure uses the dot operator and member name (instead of square brackets and index values) to identify specific elements. For example, you can initialize only the Member values of the book structure as follows:
Struct book surprise = {
. Value = 10.99
};
You can use the specified initialization project in any order:
Struct Book Gift = {
. Value = 25.99,
. Author = "James broadfool ",
. Title = "rue for the toad"
};
Like an array, a general initialization project after a specified initialization project provides an initial value for the member after the specified member. In addition, the last value assigned to a specific member is the actually obtained value. For example, consider the following statement:
Struct Book Gift = {
. Value = 18.90,
. Author = "philionna pestle ",
0.25 // value = 0.25
};
This will assign the value 0.25 to the member value because it follows the author member in the structure declaration. The new value 0.25 replaces the previous value 18.90.
For more information about designated initializer, see section 6.7.8 ininialization of the c99 standard.
Among them, VC ++ 6.0 only supports c89 initialization, and GCC supports self-marking or self-extension initialization. This type of initialization uses. Name = value. In this way, you do not need to sort the values in order, so that you can easily adjust the actual values and avoid any inconvenience caused by misplacement or definition adjustment in a large structure.
Struct name_str {
Int data;
Char name [120];
Int num;
};
/* Mark-based initialization. Note that the order is different and the default value can be used.
*/
Struct name_str STR = {
. Num = 100;
. Name = "hxy ";
};
/* C89 initialization */
Struct name_str str2 =
{
100, "Andrew Huang",-2
};
/* GCC extension initialization */
Struct name_str str3 =
{
Name: "bluedrum ";
Data:-1
}
In the linux2.6 kernel, the definition form of struct has changed, and the original definition form is no longer supported.
Static struct file_operations test_ops = {
. Open = test_open,
. Read = test_read
. Write = test -- write,
};
This statement uses the tag structure initialization syntax. This writing method is worth using because it makes the driver more portable when the schema definition changes, and makes the code more compact and easy to read. The tagged initialization method allows structure members to be rearranged. In some cases, storing frequently accessed members on the same hardware cache line greatly improves performance.